膨胀时,RelativeLayout 不遵守设置的宽度
我有一个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>
有办法解决这个问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
膨胀
RelativeLayout
时,请勿使用其中的inflate()
方法签名。相反,请使用inflate()
将父容器作为第二个参数,将布尔值作为第三个参数。在第二个参数中提供RelativeLayout
的最终父级,如果您不希望立即添加RelativeLayout
,请将false
作为第三个参数传递。至少,当使用
RelativeLayout
作为ListView
中的行的基础时,此秘籍可以解决各种RelativeLayout
膨胀问题。When inflating a
RelativeLayout
, do not use theinflate()
method signature that you have there. Instead, use theinflate()
that takes the parent container as the 2nd parameter and a boolean as the third. Supply the eventual parent for theRelativeLayout
in the 2nd parameter, and if you do not want theRelativeLayout
added immediately, passfalse
as the 3rd parameter.Leastways, this recipe clears up all sorts of
RelativeLayout
inflation problems when using aRelativeLayout
as the basis for a row in aListView
.