简体   繁体   中英

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. When I specify the whole layout and it's children in xml, this works. However, when I try to inflate the xml to add more views (code below), the Layout fills the width of the screen.

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

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

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. Instead, use the inflate() that takes the parent container as the 2nd parameter and a boolean as the third. 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.

Leastways, this recipe clears up all sorts of RelativeLayout inflation problems when using a RelativeLayout as the basis for a row in a ListView .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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