如何为包含的布局设置重量?

时间:2013-10-29 07:11:10

标签: android android-layout android-layout-weight

我有两个xml文件,页眉和页脚如下所示

header.xml

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#9d1246" >

        <Button
            android:id="@+id/button_backfromreg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/backbtn"
            android:visibility="visible" />
    </LinearLayout>

</LinearLayout>

footer.xml

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#9d1246" >
    </LinearLayout>

</LinearLayout>

我在mainlayout中包含了上面两个布局,如图所示

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#eaedf2"
    android:orientation="vertical" >

    <include
        android:layout_weight="1"
        layout="@layout/header" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="30dp"
        android:layout_weight="1"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:editable="false"
            android:ems="10"
            android:hint="Country"
            android:textColor="#655e5e"
            android:textSize="15sp" >

            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/editText2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:editable="false"
            android:ems="10"
            android:hint="State"
            android:textColor="#655e5e"
            android:textSize="15sp" />

        <EditText
            android:id="@+id/editText3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:editable="false"
            android:ems="10"
            android:hint="Place Keyword"
            android:textColor="#655e5e"
            android:textSize="15sp" />
    </LinearLayout>

    <include
        android:layout_height="0dp"
        android:layout_weight="1"
        layout="@layout/footer" />

</LinearLayout>

我的问题是,使用上面的布局我看不到页脚。有人请帮助我。 提前谢谢。

3 个答案:

答案 0 :(得分:15)

将包含的布局保留在容器中,并仅将权重属性设置为该容器。

这是您更新后的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#eaedf2"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <include

            android:layout_height="match_parent"
            android:layout_width="match_parent"
            layout="@layout/header" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="30dp"
        android:layout_weight="1"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:editable="false"
            android:ems="10"
            android:hint="Country"
            android:textColor="#655e5e"
            android:textSize="15sp" >

            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/editText2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:editable="false"
            android:ems="10"
            android:hint="State"
            android:textColor="#655e5e"
            android:textSize="15sp" />

        <EditText
            android:id="@+id/editText3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:editable="false"
            android:ems="10"
            android:hint="Place Keyword"
            android:textColor="#655e5e"
            android:textSize="15sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <include
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            layout="@layout/footer" />
    </LinearLayout>

</LinearLayout>

答案 1 :(得分:0)

在你的页脚中设置match_parent为高度..

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#9d1246" >
</LinearLayout>

答案 2 :(得分:-2)

通过将weightSum = "3"提供给parentLayout

来尝试此操作
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#eaedf2"
    android:orientation="vertical"
    android:weightSum="3"
 >

并在include

添加参数
<include
         android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        layout="@layout/header" />


<include
         android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        layout="@layout/footer" />

希望这有效