RelativeLayout和高度百分比。无法使用layout_below和layout_weight

时间:2012-10-16 09:33:30

标签: android android-layout

萨吕!我对relativelayout有问题,其子节点可以使用layout_below和layout_weight来设置其百分比高度。

例如。我们有相对布局,在内部我们有3个线性布局和textview。现在,我想设置3个(线性布局)相对布局总高度的25,50,25%。我怎么能成功呢?

我找到了weight_sum and and relativelayout (CLICK)的解决方案,但它没有使用layout_below。

请帮忙。我尝试在所有linearlayouts中添加额外的linearlayout,但后来我无法使用layout_below。

这是概念图片的想法: enter image description here

这是我的代码:

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/linlay_leftcol_notification_header" >

<LinearLayout
android:id="@+id/linlay_leftcol_clock_theday"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<TextView
    android:id="@+id/tv_theday"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/theday"
    android:textColor="#FFFFFF" />
</LinearLayout>

<LinearLayout
android:id="@+id/linlay_leftcol_clock_thetime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/linlay_leftcol_clock_theday" >

<TextView
    android:id="@+id/tv_thetime"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/thetime"
    android:textColor="#FFFFFF" />
</LinearLayout>

<LinearLayout
android:id="@+id/linlay_leftcol_clock_thenumber_themonth_theyear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/linlay_leftcol_clock_thetime" >

<TextView
    android:id="@+id/tv_thenumberofday_themonth_theyear"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/thenumberofday_themonth_theyear"
    android:textColor="#FFFFFF" />
</LinearLayout>
</RelativeLayout>

P.S。请原谅语法,因为我还在学习这门语言。我在帖子之前搜索了解决方案。

4 个答案:

答案 0 :(得分:11)

试试这个......希望这能解决目的......: - )

<?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="match_parent"
    android:background="#ff00ff"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_weight="1"
        android:background="#ffff00"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/textView0"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="Hello 1"
            android:textColor="#000000"
            android:textSize="15dp" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_weight="2"
        android:background="#00ffff"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="Hello 2"
            android:textColor="#000000"
            android:textSize="15dp" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_weight="1"
        android:background="#ffff00"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/textView2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="Hello 3"
            android:textColor="#000000"
            android:textSize="15dp" />
    </RelativeLayout>
</LinearLayout>

答案 1 :(得分:3)

检查这个::

<?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="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight=".15"
        android:background="@android:color/background_dark"
        android:orientation="vertical" >

        <RelativeLayout
            android:id="@+id/rel_1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#A52A2A" >

            <TextView
                android:id="@+id/tv_theday"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="1st layout"
                android:textColor="#FFFFFF" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rel_2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#8B008B" >

            <TextView
                android:id="@+id/tv_theday"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="2ndlayout"
                android:textColor="#FFFFFF" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rel_3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#00008B" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="3rd layout"
                android:textColor="#FFFFFF" />
        </RelativeLayout>
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/rel_side"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight=".75"
        android:background="#ff0000" >

        <TextView
            android:id="@+id/tv_theday"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="side layout"
            android:textColor="#FFFFFF" />
    </RelativeLayout>

</LinearLayout>

答案 2 :(得分:0)

您可以使用相对布局属性的任一权重。您可以将这个25,50,25列为这个...您将父对齐的第一个textview,对齐父对象,其他textview以及您对齐到第一个左边和最后一个右边的剩余textview。

答案 3 :(得分:0)

使用表格布局,在其中,您可以放置​​文本视图。表布局有一个属性“拉伸列”,你可以在那里提到大小为“0,2,1”......