为什么我不能覆盖Android项目中包含布局的布局属性?

6
我正在尝试创建一些模块化UI元素,如Android Developers - Creating Reusable UI Components中所述,不幸的是我没有得到期望的结果。该文章指出:

您可以覆盖所有布局参数。这意味着任何android:layout_*属性都可以与< include >标记一起使用。

我的XML代码如下(相关部分):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">

<include android:id="@+id/radio_group"
         layout="@layout/radio_buttons"
         android:layout_marginTop="75dp"
        />

<include android:id="@+id/player_group"
         layout="@layout/player_buttons"
         android:layout_alignParentBottom="true"
        />


<EditText android:id="@+id/text_field"
    android:singleLine="true"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Enter Your Login ID:"
    android:layout_below="@id/player_group"
    />

这是包含在其中的第一个布局:

<RadioGroup
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/radio_group"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginBottom="20dp"
    >

<RadioButton android:id="@+id/radio01"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="Song #1"
             android:paddingLeft="45dp"
        />

[two more identical buttons]

<TextView   android:id="@+id/choice"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="you have selected:"/>

</RadioGroup>

还有另外一个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

<Button android:text="btn01"
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        />
<Button android:text="@+id/Button02"
        android:id="@+id/Button02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        />

<Button android:text="Start Playing"
        android:id="@+id/startPlayerBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:onClick="doClick"
        />


</LinearLayout>

不幸的是 - 这会导致所有包含的UI元素都挤在屏幕顶部。EditText元素对齐正确,但没有任何一个包含的元素会响应任何重写的属性。我尝试了许多不同的属性,但结果都一样。
注意:我还尝试在包含的布局中添加android:layout_alignParentBottom="true"参数,以为可能需要在其中才能被覆盖,但结果仍然一样。
另外,marginTop参数也没有起到任何作用。
所以,你有什么想法吗?这让我疯狂!
注意:之前有人问过如何样式化包含的布局的问题,答案是参考可重用UI组件页面。只想说我已经阅读了那个问题和答案,但并没有帮助。

你能发布包含的布局吗? - goto10
1个回答

17

使用include时会出现一个bug,除非您为android:layout_widthandroid:layout_height指定值,否则包含的布局位置不正确。尝试这样做:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">

    <include android:id="@+id/radio_group"
         layout="@layout/radio_buttons"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_marginTop="75dp"
        />

    <include android:id="@+id/player_group"
         layout="@layout/player_buttons"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_below="@id/radio_group"
        />

    <EditText android:id="@+id/text_field"
        android:singleLine="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Your Login ID:"
        android:layout_below="@id/player_group"
        />

很有意思 - 我在谷歌上努力搜索了这个问题,但是没有找到您提到的谷歌代码页面。如果他们将此错误报告集成到可重用UI组件链接中,那就太好了。 - Bennett Von Bennett

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接