线性布局不等宽

Linear layout divide not equally width

我知道如何将布局分成 3 或 4 个相等的部分,但是如何将线性布局(水平)分成 4 个部分?

如您所见,部件号“1”是部件“3”和“4”的 3 倍。 相反,部件号“2”是部件“3”和“4”的 2 倍。

我试过这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/edColore"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="4"
        android:background="@android:color/white"
        android:text="ARANCIONE" />

    <TextView
        android:id="@+id/edGiorno"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="@android:color/holo_green_light"
        android:text="Button" />

    <TextView
        android:id="@+id/edOra"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/holo_blue_light"
        android:text="Button" />

    <TextView
        android:id="@+id/edPosizione"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/holo_red_light"
        android:text="Button" />

</LinearLayout>

但不要如我所愿:

你应该使用android:weightSum

Defines the maximum weight sum. If unspecified, the sum is computed by adding the layout_weight of all of the children. This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0.

你的XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="8" >

        <TextView
            android:id="@+id/edColore"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="4"
            android:background="@android:color/white"
            android:text="ARANCIONE" />

        <TextView
            android:id="@+id/edGiorno"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="@android:color/holo_green_light"
            android:text="Button" />

        <TextView
            android:id="@+id/edOra"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/holo_blue_light"
            android:text="Button" />

        <TextView
            android:id="@+id/edPosizione"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/holo_red_light"
            android:text="Button" />

</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="wrap_content"
    android:orientation="horizontal"
    android:weightSum="10">

    <TextView
        android:id="@+id/edColore"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="5"
        android:background="@android:color/white"
        android:text="ARANCIONE" />

    <TextView
        android:id="@+id/edGiorno"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:background="@android:color/holo_green_light"
        android:text="Button" />

    <TextView
        android:id="@+id/edOra"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/holo_blue_light"
        android:text="Button" />

    <TextView
        android:id="@+id/edPosizione"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/holo_red_light"
        android:text="Button" />

</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="wrap_content"
android:orientation="horizontal">

    <TextView
    android:id="@+id/edColore"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="3"
    android:background="@android:color/white"
    android:text="ARANCIONE" />

<TextView
    android:id="@+id/edGiorno"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2"
    android:background="@android:color/holo_green_light"
    android:text="Button" />

<TextView
    android:id="@+id/edOra"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="@android:color/holo_blue_light"
    android:text="Button" />

<TextView
    android:id="@+id/edPosizione"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="@android:color/holo_red_light"
    android:text="Button" />
</LinearLayout>