LinearLayout одинаково заполняет текстовые представления заданным значением пространства - PullRequest
0 голосов
/ 15 февраля 2019

У меня есть требования к дизайну, которые заключаются в следующем:

Есть три текстовых представления с переменным текстом, и расстояние между ними составляет 10 dp.Текстовые представления и разделители должны быть частью пустого контейнера, ширина которого равна ширине экрана.

Ниже изображения, которое соответствует требованиям:

enter image description here

В моей реализации я использую LinearLayout, который соответствует родительской ширине, текстовые представления имеют вес, равный 1, и пространственные представления между контейнером и текстовыми представлениями, вес которых равен 1.

Моя проблема в том, что текстовые представления не равны на экране из-за разной длины текста.Ниже моего определения LinearLayout.

enter image description here

Мой вопрос: Возможно ли написать представление 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>

Ответы [ 2 ]

0 голосов
/ 15 февраля 2019

Для всех View, которые вы назначаете weight, вы должны установить:

android:layout_width="0dp"

, но проблема в том, что таким образом все Space s и TextView s будут иметьтакой же ширины.Я изменил android:weightSum с 7 до 19 и дал вес 5 каждому TextView.Так:

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>
0 голосов
/ 15 февраля 2019

Используйте layout_width='0dp' с TextView с, если вы используете веса с горизонтальным LinearLayout.

Кроме того, если вам нужно использовать 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>
...