【问题标题】:constraint layout - Two views with largest width约束布局 - 具有最大宽度的两个视图
【发布时间】:2020-03-20 22:22:44
【问题描述】:

我想创建一个布局(使用约束布局),如下所示:

在不同的语言中,Button1 可能大于 button2。我该怎么做?

我只能在包含两个按钮的约束内使用 LinearLayout 来实现这一点,但我试图只使用一个布局。

谢谢

【问题讨论】:

  • 发布您的适用于线性布局的 xml !我会帮你把它转换成约束布局。

标签: android android-layout android-constraintlayout


【解决方案1】:

根据我从您的问题中了解到的情况,以下代码将对您有所帮助。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello world" 
        app:layout_constraintBottom_toTopOf="@+id/text2"
        app:layout_constraintVertical_chainStyle="packed"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello world"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/text1" />

</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

    【解决方案2】:

    更新

    我想提一下,Remq 的答案有效并解决了我在下面提到的自我引用问题,其中视图的宽度由使用与引用 id 相同的视图的屏障的位置确定。关键似乎是两个视图的 app:layout_constraintWidth_min="wrap" 规范。我不清楚为什么这会起作用,或者它会继续起作用,但我想记下它

    更新 #2

    我又看看为什么 Remq 的答案有效。我的经验是,如果没有为视图指定app:layout_constraintWidth_min="wrap",两个视图都会折叠到零宽度。一旦视图的宽度为零,app:layout_constraintWidth_min="wrap" 就会再次增长它们,以便包装内容。这只是我的猜测,没有证据证明这是正在发生的事情,但这是有道理的。



    我曾多次在 Stack Overflow 上看到过类似的问题。这些问题从来没有一个令人满意的答案IMO(包括我已经回答的问题。)困难在于存在依赖性问题,因为一个视图取决于另一个视图的宽度,而另一个视图取决于第一个视图的宽度。我们陷入了参照的困境。 (以编程方式强制宽度始终是一种选择,但似乎不可取。)

    另一种可能更好的方法是使用自定义 ConstraintHelper,它将检查引用视图的大小并将所有视图的宽度调整为最大宽度。

    自定义 ConstraintHelper 放置在布局的 XML 中,并引用受影响的视图,如以下示例布局所示:

    activity_main

    <androidx.constraintlayout.widget.ConstraintLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:gravity="center"
            android:text="Button1"
            app:layout_constraintBottom_toTopOf="@+id/button2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_chainStyle="packed" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Button2 may be larger"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/button1" />
    
        <com.example.constraintlayoutlayer.GreatestWidthHelper
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:constraint_referenced_ids="button1,button2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    自定义的ConstraintHelper如下所示:

    GreatestWidthHelper

    class GreatestWidthHelper @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null,
        defStyleAttr: Int = 0
    ) : ConstraintHelper(context, attrs, defStyleAttr) {
    
        override fun updatePostMeasure(container: ConstraintLayout) {
            var maxWidth = 0
    
            // Find the greatest width of the referenced widgets.
            for (i in 0 until this.mCount) {
                val id = this.mIds[i]
                val child = container.getViewById(id)
                val widget = container.getViewWidget(child)
                if (widget.width > maxWidth) {
                    maxWidth = widget.width
                }
            }
    
            // Set the width of all referenced view to the width of the view with the greatest width.
            for (i in 0 until this.mCount) {
                val id = this.mIds[i]
                val child = container.getViewById(id)
                val widget = container.getViewWidget(child)
                if (widget.width != maxWidth) {
                    widget.width = maxWidth
    
                    // Fix the gravity.
                    if (child is TextView && child.gravity != Gravity.NO_GRAVITY) {
                        // Just toggle the gravity to make it right.
                        child.gravity = child.gravity.let { gravity ->
                            child.gravity = Gravity.NO_GRAVITY
                            gravity
                        }
                    }
                }
            }
        }
    }
    

    布局显示如下。

    【讨论】:

    • 谢谢!但是,在 ContraintLayout 轻松支持之前,我将使用 LinearLayout 解决方案。
    • @Mateu LinearLayout 解决方案是可以接受的 (IMO),但有些人会反对离开平面布局。
    • 与其创建一个冗长的bullcr*p 只是为了在ConstraintLayout 中工作,最好避免使用它
    【解决方案3】:

    我偶然发现了这个问题,并能够使用约束布局的1.1.0-beta1 中引入的androidx.constraintlayout.widget.Barrier 来解决它。所以我认为与遇到相同问题的人分享会很好:

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:gravity="center"
            android:text="Button1"
            app:layout_constraintBottom_toTopOf="@+id/button2"
            app:layout_constraintEnd_toEndOf="@id/barrierEnd"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_chainStyle="packed"
            app:layout_constraintWidth_min="wrap" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Button2 may be larger"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="@id/barrierEnd"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/button1"
            app:layout_constraintWidth_min="wrap" />
    
        <androidx.constraintlayout.widget.Barrier
            android:id="@+id/barrierEnd"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:barrierDirection="end"
            app:constraint_referenced_ids="button1,button2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    无法将它们水平居中,所以如果有人发现我会更新这个例子。

    【讨论】:

    • 救了我的朋友!!