LinearLayout 用给定的 space 值平均填充文本视图

LinearLayout fill equally textviews with given space value

我有一个设计要求如下:

有三个文本视图,文本是可变的,它们之间的 space 是 10dp。文本视图和 spacers 必须是宽度等于屏幕宽度的空白容器的一部分。

下面一张代表要求的图片:

在我的实现中,我使用与父宽度匹配的 LinearLayout,文本视图的权重等于 1,容器和文本视图之间的 space 视图的权重等于 1。

我的问题是由于文本长度不同,屏幕上的文本视图不相等。在我的 LinearLayout 定义下方。

我的问题是:是否可以按照设计者的要求编写 LinearLyout 视图,或者我应该以编程方式编写它?

<LinearLayout
        android:layout_width="0dp"
        android:layout_height="80dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:id="@+id/view_infrastructure_status"
        app:layout_constraintTop_toBottomOf="@+id/separator_under_course_list"
        android:gravity="center_horizontal|center_vertical"
        android:orientation="horizontal"
        android:weightSum="7"

        >

    <Space
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

    <TextView
            android:layout_width="wrap_content"
            android:text="Parking \n open"
            android:layout_height="wrap_content"
            android:textAlignment="gravity"
            android:layout_weight="1"
            android:paddingTop="8dp"
            android:paddingBottom="8dp"
            android:background="@drawable/bng_button"
            android:gravity="center_horizontal|center_vertical"/>

    <Space
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

    <TextView
            android:layout_width="wrap_content"
            android:text="Restaurant\nopen"
            android:layout_height="wrap_content"
            android:textAlignment="gravity"
            android:layout_weight="1"
            android:paddingTop="8dp"
            android:paddingBottom="8dp"
            android:background="@drawable/bng_button"
            android:gravity="center_horizontal|center_vertical"/>

    <Space
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

    <TextView
            android:layout_width="wrap_content"
            android:text="Shop\nopen"
            android:layout_height="wrap_content"
            android:textAlignment="gravity"
            android:layout_weight="1"
            android:paddingTop="8dp"
            android:paddingBottom="8dp"
            android:background="@drawable/bng_button"
            android:gravity="center_horizontal|center_vertical"/>

    <Space
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
</LinearLayout>

如果您在水平 LinearLayout 中使用权重,请将 layout_width='0dp'TextView 一起使用。

此外,如果您必须使用 Space,请在分配权重时注意文本视图与 space 的比率。例如,在您的情况下,权重分布可能是 space 的权重为 1,文本视图的权重为 3。相应地更改 layout_weightsum 的值。

示例 xml 代码:

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="80dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:id="@+id/view_infrastructure_status"
    app:layout_constraintTop_toBottomOf="@+id/separator_under_course_list"
    android:gravity="center_horizontal|center_vertical"
    android:orientation="horizontal"
    android:weightSum="xx">
    <Space
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

    <TextView
        android:layout_width="0dp"
        android:text="Parking \n open"
        android:layout_height="wrap_content"
        android:textAlignment="gravity"
        android:layout_weight="3"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:background="@drawable/bng_button"
        android:gravity="center_horizontal|center_vertical"/>

    ....
</LinearLayout>

对于您分配 weight 的所有 View,您必须设置:

android:layout_width="0dp"

但问题是这样所有的 Spaces 和 TextViews 将具有相同的宽度。
我将 android:weightSum 从 7 更改为 19,并为每个 TextView 赋予权重 5。
所以:

1(Space) + 5(TextView) + 1(Space) + 5(TextView) + 1(Space) + 5(TextView) + 1(Space) = 19

你可以随意调整。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="80dp"
    app:layout_constraintTop_toBottomOf="@+id/separator_under_course_list"
    android:id="@+id/view_infrastructure_status"
    android:gravity="center_horizontal|center_vertical"
    android:orientation="horizontal"
    android:weightSum="19">

    <Space
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

    <TextView
        android:layout_width="0dp"
        android:text="Parking \n open"
        android:layout_height="wrap_content"
        android:textAlignment="gravity"
        android:layout_weight="5"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:background="@drawable/bng_button"
        android:gravity="center_horizontal|center_vertical"/>

    <Space
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

    <TextView
        android:layout_width="0dp"
        android:text="Restaurant\nopen"
        android:layout_height="wrap_content"
        android:textAlignment="gravity"
        android:layout_weight="5"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:background="@drawable/bng_button"
        android:gravity="center_horizontal|center_vertical"/>

    <Space
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

    <TextView
        android:layout_width="0dp"
        android:text="Shop\nopen"
        android:layout_height="wrap_content"
        android:textAlignment="gravity"
        android:layout_weight="5"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:background="@drawable/bng_button"
        android:gravity="center_horizontal|center_vertical"/>

    <Space
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
</LinearLayout>