Android 安卓布局重量

Android 安卓布局重量,android,android-relativelayout,android-linearlayout,Android,Android Relativelayout,Android Linearlayout,也许我误解了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" andr

也许我误解了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>

在线性布局中,我有三个或更多文本视图。 我想把它们放在宽度为百分比的列中。 这就是它产生的结果:

布局重量设置为0.3/0.2/0.5

然后我改变了布局权重参数,如下所示:

<?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


这是一个bug还是我真的误解了布局权重的全部内容?

当你使用
android:layout\u-weight
时,你应该设置
android:layout\u-width=“0dp”
(如果垂直方向-
android:layou-height=“0dp”

试试这个。。。。


尝试将android:weightSum=“1”更改为android:weightSum=“1.0”权重和应为浮点值。
        <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>