简体   繁体   English

充气时RelativeLayout不遵守设置的宽度

[英]RelativeLayout does not adhere to set width when inflated

I have a relativeLayout that I would like to use the theme.dialog android theme, it should have a set width of 240dip. 我有一个relativeLayout,我想使用theme.dialog android主题,它应该有一个240dip的设置宽度。 When I specify the whole layout and it's children in xml, this works. 当我指定整个布局并且它是xml中的子项时,这是有效的。 However, when I try to inflate the xml to add more views (code below), the Layout fills the width of the screen. 但是,当我尝试给xml充气以添加更多视图(下面的代码)时,布局会填充屏幕的宽度。

Context context = this;
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    RelativeLayout header = (RelativeLayout) inflater.inflate(R.layout.headphonepopupheader, null);

headphonepopup.xml: headphonepopup.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="240dp"
android:layout_height="wrap_content" >

<ImageButton
    android:id="@+id/closebutton"
    android:layout_width="24dp"
    android:layout_height="24dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="216dp"
    android:background="@drawable/closebutton" />

</RelativeLayout>

Is there a way to solve this? 有办法解决这个问题吗?

When inflating a RelativeLayout , do not use the inflate() method signature that you have there. 在给RelativeLayout充气时,不要使用你在那里的inflate()方法签名。 Instead, use the inflate() that takes the parent container as the 2nd parameter and a boolean as the third. 相反,使用inflate()将父容器作为第二个参数,将boolean作为第三个参数。 Supply the eventual parent for the RelativeLayout in the 2nd parameter, and if you do not want the RelativeLayout added immediately, pass false as the 3rd parameter. 在第二个参数中为RelativeLayout最终父级,如果不想立即添加RelativeLayout ,则将false作为第三个参数传递。

Leastways, this recipe clears up all sorts of RelativeLayout inflation problems when using a RelativeLayout as the basis for a row in a ListView . 最少,当使用RelativeLayout作为ListView中行的基础时,此配方会清除所有类型的RelativeLayout通胀问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM