【问题标题】:ALIGN_PARENT_BOTTOM not working with my iconALIGN_PARENT_BOTTOM 不适用于我的图标
【发布时间】:2014-03-28 00:31:56
【问题描述】:

我试图以编程方式在相对布局的底部定义一个线性布局,所以我使用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);




            }
        });

这里是包含 relativeLayout 的 XML:

    <?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>

知道为什么这不能正常工作吗?

【问题讨论】:

  • 尝试使用 MATCH_PARENT 作为父母身高
  • 布局中还有什么?
  • @LucianNovac 你说的是哪种布局?
  • 生病的亲戚
  • @LucianNovac 不,它没有解决问题

标签: android android-layout android-relativelayout layoutparams


【解决方案1】:

我终于弄清楚出了什么问题。我没有设置我为布局定义的参数,对于图像也是如此。以下行丢失:iconsLinearLayout.setLayoutParams(illp);

现在可以正常使用了!

【讨论】:

    【解决方案2】:

    您应该在添加视图之前设置布局参数。

    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);
    

    【讨论】:

    • 也试过了,还是不行。。好像线性布局不是相对布局的子级。。
    猜你喜欢
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 2021-02-20
    • 2020-11-20
    • 1970-01-01
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多