Android 如何在ConstraintLayout上将版面宽度百分比设置为三分之一?
我想在一个由3x6个文本视图组成的二维网格中填充屏幕。这意味着每个文本视图必须是全屏宽度的1/3,高度必须是全屏高度的1/6。我正在为此布局使用ConstraintLayout,因为线性布局不适合此布局,因为嵌套权重不利于性能。目前,我使用百分比0.333333和0.166666667分别表示宽度和高度,如下所示Android 如何在ConstraintLayout上将版面宽度百分比设置为三分之一?,android,android-xml,android-constraintlayout,Android,Android Xml,Android Constraintlayout,我想在一个由3x6个文本视图组成的二维网格中填充屏幕。这意味着每个文本视图必须是全屏宽度的1/3,高度必须是全屏高度的1/6。我正在为此布局使用ConstraintLayout,因为线性布局不适合此布局,因为嵌套权重不利于性能。目前,我使用百分比0.333333和0.166666667分别表示宽度和高度,如下所示 <TextView app:layout_constraintLeft_toLeftOf="parent" app:layout_constrai
<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 您应该像绑定到父视图的开始和后续视图的开始一样使用链。然后将所有视图的宽度设置为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>
@MohammadMoeinGolchin你能给我举个例子吗?对不起,可以给它加1/3和1/6,但它会像1一样工作,并且会被忽略。是的,的确如此。因此,问题是,我们如何做到这一点?像你那样使用0.333有什么问题?@MohammadMoeinGolchin没有,还没有,但我想我还是会使用线性布局……问题在于我们有嵌套权重,这对性能不利。这就是我想使用ConstraintLayout的原因。为什么它不好@t、 例如,请参见下面的链接@t.r.hogenkamp