Автоматическое размещение дочерних элементов LinearLayout с фиксированной шириной - PullRequest
0 голосов
/ 03 августа 2020

У меня есть строка текста, которую я использую LinearLayout для представления:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="A"
            tools:ignore="HardcodedText" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="B"
            tools:ignore="HardcodedText" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="C"
            tools:ignore="HardcodedText" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="D"
            tools:ignore="HardcodedText" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="E"
            tools:ignore="HardcodedText" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="F"
            tools:ignore="HardcodedText" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="G"
            tools:ignore="HardcodedText" />
    </LinearLayout>

Я хочу, чтобы эти буквы были равномерно распределены в LinearLayout (вроде как поле auto в CSS); прямо сейчас они все как бы сбиты вместе. Как я могу выполнить sh это?

Ответы [ 2 ]

0 голосов
/ 03 августа 2020
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="A"
        tools:ignore="HardcodedText" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="B"
        tools:ignore="HardcodedText" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="C"
        tools:ignore="HardcodedText" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="D"
        tools:ignore="HardcodedText" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="E"
        tools:ignore="HardcodedText" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="F"
        tools:ignore="HardcodedText" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="G"
        tools:ignore="HardcodedText" />
</LinearLayout>

Я предлагаю использовать вид горизонтальной прокрутки в качестве родительского для прокрутки слева направо

0 голосов
/ 03 августа 2020

Это легко сделать, установив layout_width для LinearLayout на 0dp и layout_weight для каждого TextView на 1.

...