Android 布局权重

3
也许我误解了layout_weight参数,但我不明白为什么像这样的东西不起作用...
<?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="horizontal" android:weightSum="1">

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="TextView" android:layout_weight="0.3" android:background="@color/apr_blue"/>

<TextView
    android:id="@+id/textView2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.2"
    android:text="TextView" 
    android:background="@color/apr_brightyellow"/>

<TextView
    android:id="@+id/textView3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:text="TextView" 
    android:background="@color/apr_yellow"/>

</LinearLayout>

我有一个LinearLayout中有三个或更多的TextView。 我想用百分比宽度将它们放在列中。 以下是它的效果:
设置layout_weight为0.3 / 0.2 / 0.5
然后我稍微改变了layout_weight参数,如下所示:
<?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="horizontal" android:weightSum="1">

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="TextView" android:layout_weight="0.3" android:background="@color/apr_blue"/>

<TextView
    android:id="@+id/textView2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.3"
    android:text="TextView" 
    android:background="@color/apr_brightyellow"/>

<TextView
    android:id="@+id/textView3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.4"
    android:text="TextView" 
    android:background="@color/apr_yellow"/>

</LinearLayout>

现在我已经按照我的意愿得到了:

使用设置为0.3 / 0.2 / 0.5的layout_weight

Android layout_weight correctly

这是一个错误还是我真的误解了layout_weight的整个概念?


尝试将android:weightSum="1"更改为android:weightSum="1.0" 权重总和应该是浮点值。 - Tarun
2个回答

12

当使用android:layout_weight属性时,应设置android:layout_width="0dp"(在垂直方向上应设置为android:layout_height="0dp")。


1

enter image description here

请尝试这个....

        <TextView
            android:background="#F2e"
            android:layout_weight="3"
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <TextView
             android:background="#F6e"
            android:layout_weight="2"
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <TextView
             android:background="#F45"
            android:layout_weight="5"
            android:id="@+id/textView3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="TextView" />

    </LinearLayout>

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