RelativeLayout的layout_weight不起作用

时间:2013-01-16 13:28:49

标签: android

有以下代码:

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="0.9"
            android:gravity="center"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/activityEvaluationYesterdayValueDisplay"
                android:layout_width="15dip"
                android:layout_height="wrap_content"
                android:text="0"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textStyle="bold" />

            <com.ulnda.mypsych.views.VerticalSeekBar
                android:id="@+id/activityEvaluationYesterdayValue"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:max="10"
                android:progress="0" />
        </LinearLayout>

现在我需要将LinearLayout更改为RelativeLayout。如果我这样做,那么RelativeLayout会填满整个屏幕,这很糟糕。我知道RelativeLayout不使用layout_weight来完成它的工作。请告诉我,我该如何解决?我试图为RelativeLayout制作LinearLayout包装器,但它没有帮助。

更新:

<?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:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="0.6"
        android:orientation="vertical" >

        <RelativeLayout
            android:background="#000000"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </RelativeLayout>

    </LinearLayout>

</LinearLayout>
UPDATE 2: FUll code
<?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:orientation="vertical" >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="0.5" >

            <LinearLayout
                android:weightSum="1.0"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="0.33"
                android:orientation="vertical" >

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dip"
                    android:layout_weight="0.9">

                    <TextView
                        android:id="@+id/activityEvaluationYesterdayValueDisplay"
                        android:layout_width="15dip"
                        android:layout_height="wrap_content"
                        android:text="0"
                        android:layout_centerVertical="true"
                        android:layout_toLeftOf="@+id/activityEvaluationYesterdayValue"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textStyle="bold" />

                    <com.ulnda.mypsych.views.VerticalSeekBar
                        android:id="@+id/activityEvaluationYesterdayValue"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_centerInParent="true"
                        android:max="10"
                        android:progress="0" />
                </RelativeLayout>

                <TextView
                    android:id="@+id/TextView05"
                    android:layout_width="match_parent"
                    android:layout_height="0dip"
                    android:layout_weight="0.1"
                    android:gravity="center"
                    android:text="Yesterday"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textStyle="bold" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="0.33"
                android:orientation="vertical" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dip"
                    android:layout_weight="0.9"
                    android:gravity="center"
                    android:orientation="horizontal" >

                    <TextView
                        android:id="@+id/activityEvaluationTodayValueDisplay"
                        android:layout_width="20dip"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:text="0"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textStyle="bold" />

                    <com.ulnda.mypsych.views.VerticalSeekBar
                        android:id="@+id/activityEvaluationTodayValue"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:max="10"
                        android:progress="0" />
                </LinearLayout>

                <TextView
                    android:id="@+id/TextView03"
                    android:layout_width="match_parent"
                    android:layout_height="0dip"
                    android:layout_weight="0.1"
                    android:gravity="center"
                    android:text="Today"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textStyle="bold" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="0.33"
                android:orientation="vertical" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dip"
                    android:layout_weight="0.9"
                    android:gravity="center"
                    android:orientation="horizontal" >

                    <TextView
                        android:id="@+id/activityEvaluationTomorrowValueDisplay"
                        android:layout_width="20dip"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:text="0"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textStyle="bold" />

                    <com.ulnda.mypsych.views.VerticalSeekBar
                        android:id="@+id/activityEvaluationTomorrowValue"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:max="10"
                        android:progress="0" />
                </LinearLayout>

                <TextView
                    android:id="@+id/TextView02"
                    android:layout_width="match_parent"
                    android:layout_height="0dip"
                    android:layout_weight="0.1"
                    android:gravity="center"
                    android:text="Tomorrow"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textStyle="bold" />
            </LinearLayout>
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="0dip"
            android:layout_weight="0.5" >

            <LinearLayout
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="0.33"
                android:orientation="vertical" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dip"
                    android:layout_weight="0.9"
                    android:gravity="center"
                    android:orientation="horizontal" >

                    <TextView
                        android:id="@+id/activityEvaluationEnergyValueDisplay"
                        android:layout_width="20dip"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:text="0"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textStyle="bold" />

                    <com.ulnda.mypsych.views.VerticalSeekBar
                        android:id="@+id/activityEvaluationEnergyValue"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:max="10"
                        android:progress="0" />
                </LinearLayout>

                <TextView
                    android:id="@+id/TextView11"
                    android:layout_width="match_parent"
                    android:layout_height="0dip"
                    android:layout_weight="0.1"
                    android:gravity="center"
                    android:text="Energy"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textStyle="bold" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="0.33"
                android:orientation="vertical" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dip"
                    android:layout_weight="0.9"
                    android:gravity="center"
                    android:orientation="horizontal" >

                    <TextView
                        android:id="@+id/activityEvaluationWeatherValueDisplay"
                        android:layout_width="20dip"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:text="0"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textStyle="bold" />

                    <com.ulnda.mypsych.views.VerticalSeekBar
                        android:id="@+id/activityEvaluationWeatherValue"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:max="10"
                        android:progress="0" />
                </LinearLayout>

                <TextView
                    android:id="@+id/TextView09"
                    android:layout_width="match_parent"
                    android:layout_height="0dip"
                    android:layout_weight="0.1"
                    android:gravity="center"
                    android:text="Weather"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textStyle="bold" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="0.33"
                android:orientation="vertical" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dip"
                    android:layout_weight="0.9"
                    android:gravity="center"
                    android:orientation="horizontal" >

                    <TextView
                        android:id="@+id/activityEvaluationHoursSleptValueDisplay"
                        android:layout_width="20dip"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:text="0"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:textStyle="bold" />

                    <com.ulnda.mypsych.views.VerticalSeekBar
                        android:id="@+id/activityEvaluationHoursSleptValue"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:max="10"
                        android:progress="0" />
                </LinearLayout>

                <TextView
                    android:id="@+id/TextView07"
                    android:layout_width="match_parent"
                    android:layout_height="0dip"
                    android:layout_weight="0.1"
                    android:gravity="center"
                    android:text="Hours Slept"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textStyle="bold" />
            </LinearLayout>
        </TableRow>
    </TableLayout>

</LinearLayout>

为什么RelativeLayout会填满整个屏幕,而不是0.6?

2 个答案:

答案 0 :(得分:6)

android:layout_weight仅对LinearLayout有意义。 Lint应该通过警告

了解你

答案 1 :(得分:1)

RelativeLayout是唯一拥有layout_weight属性的孩子,因此您还应指定weightSum

<?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:orientation="vertical"
    android:weightSum="1">

    <RelativeLayout
        android:background="#ff0000"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="0.6">

    </RelativeLayout>
</LinearLayout>