如何在ConstraintLayout上将布局宽度百分比设置为三分之一?

时间:2020-04-28 09:37:04

标签: android android-xml android-constraintlayout

我想在3x6 TextViews的二维网格中填充屏幕。这意味着每个TextView必须为全屏宽度的1/3和高度为全屏高度的1/6。我正在为此布局使用ConstraintLayout,因为LinearLayouts对于该布局是不好的做法,因为嵌套权重对性能不利。当前,我分别使用百分比0.333333333333333333和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 :(得分:0)

您应该使用像A这样的链绑定到父视图的开始和以下视图的开始。然后,将所有视图的宽度设置为0。可以将其包含在所有行中。

然后,您可以使用宽高比使宽度和高度相同。

答案 1 :(得分:0)

    <?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>