如何在ConstraintLayout上将布局宽度百分比设置为三分之一? [英] How to set a layout width percentage of one third on ConstraintLayout?
问题描述
我想在3x6 TextViews的二维网格中填充屏幕.这意味着每个TextView必须为全屏宽度的1/3和高度为全屏高度的1/6.我将ConstraintLayout用于此布局,因为LinearLayouts对于此布局是不正确的做法,因为嵌套权重不利于性能.目前,我分别使用百分比0.333333333333333333和0.166666666666667表示宽度和高度,如下所示.
I want to fill the screen in a two-dimensional grid of 3x6 TextViews. This means that each TextView must be 1/3 of the full screen width and the height 1/6 of the full screen height. I am using a ConstraintLayout for this layout, since LinearLayouts are bad practise for this layout as nested weights are bad for performance. Currently, I am using the percentage 0.333333333333333 and 0.166666666666667 for respectively the width and height, as shown below.
<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?
I was wondering whether it is possible to set the percentages as fractions? Thus for the width 1/3 and the height 1/6?
推荐答案
<?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>
这篇关于如何在ConstraintLayout上将布局宽度百分比设置为三分之一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!