[英]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.