簡體   English   中英

充氣時RelativeLayout不遵守設置的寬度

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

我有一個relativeLayout,我想使用theme.dialog android主題,它應該有一個240dip的設置寬度。 當我指定整個布局並且它是xml中的子項時,這是有效的。 但是,當我嘗試給xml充氣以添加更多視圖(下面的代碼)時,布局會填充屏幕的寬度。

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>

有辦法解決這個問題嗎?

在給RelativeLayout充氣時,不要使用你在那里的inflate()方法簽名。 相反,使用inflate()將父容器作為第二個參數,將boolean作為第三個參數。 在第二個參數中為RelativeLayout最終父級,如果不想立即添加RelativeLayout ,則將false作為第三個參數傳遞。

最少,當使用RelativeLayout作為ListView中行的基礎時,此配方會清除所有類型的RelativeLayout通脹問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM