android - RelativeLayout 在膨胀时不遵守设置的宽度

标签 android android-relativelayout layout-inflater

我有一个 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/

相关文章:

android - 在 Chrome 中访问位置,在 WebView 中不起作用

android - 根据背景图片相对布局换行宽高

android - 相对布局 - android - 使相对布局可滚动

java - 如何让 android LayoutInflater 使用我的自定义资源

android - 22.1+ 支持库不调用 LayoutInflater.Factory onCreateView

带有单选按钮的Android可扩展 ListView

java - 在 OnPostExecute() 中使用 StartActivity

android - Windows 7 下的 adb shell 自动完成

android - 将抽屉导航的 ListView 更改为任何自定义 View (比如相对布局)?

java - 在 glOrtho View 中绘制一条线,为什么它使用默认投影?