我正在尝试以编程方式在相对布局的底部定义linearlayout,因此我使用的是ALIGN_PARENT_BOTTOM
。但是创建的布局显示在我的屏幕顶部,好像我没有定义它的位置。
这是java代码:
final RelativeLayout rootRelativeLayout = (RelativeLayout) findViewById(R.id.relative_layout);
//find view elements
image = (ImageView) findViewById(R.id.image_id);
image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Adding the delete icon and text
Log.i("click", "I clicked ! ");
// Creating a new LinearLayout that will contain the icons
/* <LinearLayout
* android:id="@+id/icons_layout"
* android:layout_width="match_parent"
* android:layout_height="wrap_content"
* android:layout_alignParentBottom="true"
* android:gravity="center"
* android:orientation="horizontal" >
*
*/
final LinearLayout iconsLinearLayout = new LinearLayout(getApplicationContext());
//attach linearLayout to root relativeLayout
rootRelativeLayout.addView(iconsLinearLayout);
iconsLinearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
final RelativeLayout.LayoutParams illp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
illp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
/* Defining the image */
/* <ImageView
* android:id="@+id/icon_id"
* android:layout_width="wrap_content"
* android:layout_height="wrap_content"
* android:src="@drawable/icon"
* android:clickable="true" />
*/
ImageView icon = new ImageView(context);
//adding the ImageView in the linearlayout
iconsLinearLayout.addView(icon);
icon.setId(R.id.icon_id);
icon.setImageResource(R.drawable.icon);
icon.setClickable(true);
// Defining the ImageView layout parameters.
/* the image is inside a linear layout --> linearLayout params */
final LinearLayout.LayoutParams dblp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
dblp.weight = 1;
dblp.gravity = Gravity.CENTER;
icon.setLayoutParams(dblp);
}
});
这里的XML包含relativeLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/shelves_layout"
android:background="@drawable/background" >
<LinearLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginLeft="70dp"
android:layout_marginRight="65dp"
android:layout_marginTop="@dimen/first_shelf_margin_top"
android:orientation="horizontal" >
<ImageView
android:id="@+id/image_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/image1"
android:clickable="true"/>
</LinearLayout>
</RelativeLayout>
知道为什么这不能正常工作?
答案 0 :(得分:1)
我终于弄清楚出了什么问题。我没有将我定义的参数设置为布局,对图像也是如此。以下几行是:iconsLinearLayout.setLayoutParams(illp);
现在它运作正常!
答案 1 :(得分:0)
您应该在添加视图之前设置layoutparams。
iconsLinearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
final RelativeLayout.LayoutParams illp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
illp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rootRelativeLayout.addView(iconsLinearLayout);