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

Ben*_*ett 6 layout android

我正在尝试创建一些模块化的UI元素,如Android开发人员所述 - 创建可重用的UI组件,遗憾的是我没有得到所需的结果.文章指出:

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

我的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"
    />
Run Code Online (Sandbox Code Playgroud)

这是第一个包含的布局:

<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>
Run Code Online (Sandbox Code Playgroud)

和另外一个:

<?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>
Run Code Online (Sandbox Code Playgroud)

不幸的是 - 这导致所有包含的UI元素在屏幕顶部彼此叠加在一起.EditText元素正确对齐,但所有包含的元素都不会响应任何覆盖的属性.我尝试了许多具有相同非结果的不同的.

注意:我还尝试将一个android:layout_alignParentBottom="true"参数放入包含的布局中,认为可能需要在其中覆盖它,但是得到了相同的非结果.

另外 - marginTop param也没有做任何事情.

那么 - 任何想法我做错了什么?这真让我抓狂!

注意:前面有一个问题是关于如何设置包含布局的样式,答案是对可重用UI组件页面的引用.只是想说我已经阅读了这个问题和答案而且没有帮助.

got*_*o10 17

除非您为和指定值,否则在使用包含布局未正确定位的位置时会出现错误.试试这个:includeandroid: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"
        />
Run Code Online (Sandbox Code Playgroud)