【问题标题】:How to set a layout width percentage of one third on ConstraintLayout?如何在 ConstraintLayout 上设置三分之一的布局宽度百分比?
【发布时间】:2020-04-28 09:37:04
【问题描述】:

我想在 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?

【问题讨论】:

  • @MohammadMoeinGolchin 你能举个例子吗?
  • 对不起,可以给它 1/3 和 1/6,但它会像 1 和 / 一样工作,并且会被忽略。
  • 是的,确实如此。那么问题来了,我们该怎么做呢?
  • 像你这样使用0.333333333333333有什么问题?
  • @MohammadMoeinGolchin 不,还没有,但我想我还是会使用 LinearLayouts...

标签: android android-xml android-constraintlayout


【解决方案1】:
    <?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 的原因。
  • 为什么不好? @t.r.hogenkamp
  • 参见例如以下link
  • 感谢链接@t.r.hogenkamp
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多