Как установить процент ширины макета на одну треть в ConstraintLayout? - PullRequest
0 голосов
/ 28 апреля 2020

Я хочу заполнить экран в двумерной сетке 3x6 TextViews. Это означает, что каждый TextView должен составлять 1/3 от полной ширины экрана и высоту 1/6 от полной высоты экрана. Я использую ConstraintLayout для этого макета, так как LinearLayouts - плохая практика для этого макета, поскольку вложенные веса ухудшают производительность. В настоящее время я использую проценты 0,333333333333333 и 0,166666666666667 соответственно для ширины и высоты, как показано ниже.

<TextView
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toLeftOf="@id/text_view_theme_remove"
        app:layout_constraintBottom_toTopOf="@id/text_view_theme_percentage"
        app:layout_constraintWidth_default="percent"
        app:layout_constraintWidth_percent="0.333333333333333"
        app:layout_constraintHeight_default="percent"
        app:layout_constraintHeight_percent="0.166666666666667"
        android:gravity="center"
        android:id="@+id/text_view_theme_unknown"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/transparent"
        app:autoSizeTextType="uniform"
        android:text="@string/unknown"/>

Мне было интересно, можно ли установить проценты в виде дробей? Таким образом, для ширины 1/3 и высоты 1/6?

Ответы [ 2 ]

0 голосов
/ 28 апреля 2020
    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <LinearLayout android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_weight="3"
        android:orientation="horizontal"
        xmlns:android="http://schemas.android.com/apk/res/android" >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="1"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="2"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="3"/>
    </LinearLayout>
    <LinearLayout android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_weight="3"
        android:orientation="horizontal"
        xmlns:android="http://schemas.android.com/apk/res/android" >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="4"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="5"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="6"/>
    </LinearLayout>
    <LinearLayout android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_weight="3"
        android:orientation="horizontal"
        xmlns:android="http://schemas.android.com/apk/res/android" >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="7"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="8"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="9"/>
    </LinearLayout>
    <LinearLayout android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_weight="3"
        android:orientation="horizontal"
        xmlns:android="http://schemas.android.com/apk/res/android" >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="10"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="11"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="12"/>
    </LinearLayout>
    <LinearLayout android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_weight="3"
        android:orientation="horizontal"
        xmlns:android="http://schemas.android.com/apk/res/android" >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="13"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="14"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="15"/>
    </LinearLayout>
    <LinearLayout android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_weight="3"
        android:orientation="horizontal"
        xmlns:android="http://schemas.android.com/apk/res/android" >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="16"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="17"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="18"/>
    </LinearLayout>
</LinearLayout>
0 голосов
/ 28 апреля 2020

Вы должны использовать цепочки типа A, привязанные к началу и началу следующего представления родителей. Затем вы устанавливаете ширину всех видов, равную 0. Вы можете сделать это со всеми строками.

Затем вы можете использовать соотношение сторон , чтобы сделать ширину и высоту одинаковыми по размеру.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...