我有一个 relativeLayout,我想使用 theme.dialog android 主题,它的宽度应该设置为 240dip。当我在 xml 中指定整个布局及其子级时,这有效。但是,当我尝试膨胀 xml 以添加更多 View (下面的代码)时,布局会填满屏幕的宽度。
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()
方法签名。相反,使用将父容器作为第二个参数并将 bool 值作为第三个参数的 inflate()
。在第二个参数中为 RelativeLayout
提供最终父级,如果您不想立即添加 RelativeLayout
,请将 false
作为第三个参数传递.
至少,当使用 RelativeLayout
作为 ListView
中一行的基础时,这个秘诀解决了各种 RelativeLayout
膨胀问题。
关于android - RelativeLayout 在膨胀时不遵守设置的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8546486/