Android 平滑滚动到底部

标签 android xml android-layout view

我正在尝试以编程方式向我的 ScrollView 添加一些 View 。这是我要添加的 View 的 xml 代码。这是用水平线性布局写的

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:padding="10dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:text="1. Sadlier ofxord "
                android:textColor="@color/text_color"
                android:textSize="16sp" />

            <ImageButton
                android:contentDescription="@string/hello_world"
                android:layout_alignParentRight="true"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_centerVertical="true"
                android:background="@drawable/delete_button"
                android:src="@drawable/delete_icon" />



        </RelativeLayout>
        <View 
            android:layout_width="fill_parent"
            android:layout_height = "2dp"
            android:background="@color/text_color"/>

这些函数创建 View

private RelativeLayout createContainerLayout(){
    RelativeLayout layout = new RelativeLayout(getApplicationContext());
    layout.setPadding(10, 10, 10, 10);
    RelativeLayout.LayoutParams params = new        RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 70);
    layout.setLayoutParams(params);
    return layout;
}

private ImageButton createDeleteImageButton (){
    ImageButton button = new ImageButton(getApplicationContext());
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50, 50);
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    params.addRule(RelativeLayout.CENTER_VERTICAL);
    button.setLayoutParams(params);
    button.setBackgroundResource(R.drawable.delete_button);
    button.setImageResource(R.drawable.delete_icon);
    return button;
}

private TextView  createSetNameText(int counter , String name){
    TextView text = new TextView(getApplicationContext());
    text.setText(counter+". "+name);
    text.setTextSize(16);
    text.setTextColor(getResources().getColor(R.color.text_color));
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    params.addRule(RelativeLayout.CENTER_VERTICAL);
    return text;
}

private View createLineView(){
    View line = new View (getApplicationContext());
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT , 2);
    line.setLayoutParams(params);
    line.setBackgroundColor(getResources().getColor(R.color.text_color));
    return line;
}

这就是我将 View 添加到 ScrollView 的方式

lLayoutContainer.addView(rLayout);
rLayout.addView(setName);
rLayout.addView(deleteButton);
lLayoutContainer.addView(line);

但我最终得到了这样的结果。第一行是由 XML 创建的行,接下来的两行是通过编程创建的。那么我的代码有什么问题呢?为什么它会创建一个高度较小的布局

enter image description here

最佳答案

对于 setPaddingLayoutParams 构造函数,您都传入了明确的大小值。这将是绝对像素大小,与屏幕密度无关。

请看getDimensionPixelOffsetgetDimensionPixelSize反而。例如:

private RelativeLayout createContainerLayout(){
    final Resources r = getResources();
    final int tenDp = r.getDimensionPixelSize(R.dimens.my_padding);
    RelativeLayout layout = new RelativeLayout(getApplicationContext());
    layout.setPadding(tenDp, tenDp, tenDp, tenDp);

    final int seventyDp = r.getDimensionPixelSize(R.dimens.my_height);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, seventyDp);
    layout.setLayoutParams(params);
    return layout;
}

关于Android 平滑滚动到底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21264262/

相关文章:

java - 如何将键转换为字符串,反之亦然

android - 在 Android 中使用具有多个布局的单个 fragment

sql-server - SSIS将具有不同类型列的表导出到平面文件中

c# - 如何在 xsd 中使用枚举

android - 在 ANDROID 中以编程方式更改图像

android - 如何禁用 DatePickerDialog 的标题?

java - 类不是抽象的,不会覆盖 OnClickListener 中的抽象方法 onClick(View)

android - Activity 泄漏了原本是 android 的窗口

xml - 如何在 Excel 2011 for Mac 中解析 XML?

android - 相对布局字段的放置不当