Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在ConstraintLayout中设置视图的最小百分比宽度_Android_Android Support Library_Android Constraintlayout - Fatal编程技术网

Android 如何在ConstraintLayout中设置视图的最小百分比宽度

Android 如何在ConstraintLayout中设置视图的最小百分比宽度,android,android-support-library,android-constraintlayout,Android,Android Support Library,Android Constraintlayout,我有两个按钮在约束布局中排列成一行。默认情况下,我需要它们均匀分布,每一个都占ConstraintLayout宽度的50%(垂直指南设置为50%,这很简单): 我觉得必须有一个组合的设置在这里,我错过了实现这一点。提前谢谢 我所知道的任何标准XML代码和标准ConstraintLayout属性都无法直接实现您想要的功能。您可能可以使用嵌套布局执行某些操作,但一般来说,使用ConstraintLayout应该避免这种情况。我认为你设计的方式很可能是未来的发展方向 但是,如果您想在XML中完成这一

我有两个
按钮
约束布局中排列成一行
。默认情况下,我需要它们均匀分布,每一个都占
ConstraintLayout
宽度的50%(垂直
指南设置为50%,这很简单):


我觉得必须有一个组合的设置在这里,我错过了实现这一点。提前谢谢

我所知道的任何标准XML代码和标准ConstraintLayout属性都无法直接实现您想要的功能。您可能可以使用嵌套布局执行某些操作,但一般来说,使用ConstraintLayout应该避免这种情况。我认为你设计的方式很可能是未来的发展方向

但是,如果您想在XML中完成这一切,您可以创建一个阴影按钮,该按钮在所有方面(位置、样式、文本等)都是主按钮的副本,但它是不可见的。然后,可以在阴影按钮和中心指引的左侧设置障碍物。可见主按钮现在可以约束到屏障,因为屏障不依赖于主按钮。主按钮将至少为ConstraintLayout宽度的1/2,但可以扩展到更大的范围

第二种方法是为主按钮创建自定义视图。此自定义视图可以实现一个属性,该属性根据ConstraintLayout的宽度指定按钮的最小宽度

按钮parent.kt的最小宽度

class ButtonMinWidthOfParent @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : MaterialButton(context, attrs) {

    private var minWidthOfParent = 0f

    init {
        context.theme.obtainStyledAttributes(
            attrs,
            R.styleable.ButtonMinWidthOfParent,
            0, 0
        ).apply {
            try {
                minWidthOfParent = getFloat(R.styleable.ButtonMinWidthOfParent_minWidthOfParent, 0f)
            } finally {
                recycle()
            }
        }
    }

    @Override
    override fun getSuggestedMinimumWidth(): Int {
        return maxOf(
            super.getSuggestedMinimumWidth(),
            ((parent as ViewGroup).measuredWidth * minWidthOfParent).toInt()
        )
    }
}
attrs.xml

<resources>
    <declare-styleable name="ButtonMinWidthOfParent">
        <attr name="minWidthOfParent" format="float"/>
    </declare-styleable>
</resources>


毫无疑问,还有其他方法,但XML方法的优点是,它是一个纯XML实现,有点小技巧,而自定义视图是扩展视图功能的更标准的方法。

您能用UI更新您的问题吗。。例如,您需要使用ConstraintLayout实现哪个UI。更新以更清楚我的问题是,如果我有主按钮的文本
“主按钮要长得多”
,我需要将宽度扩展到50%以上,然后将辅助按钮推到左侧。
class ButtonMinWidthOfParent @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : MaterialButton(context, attrs) {

    private var minWidthOfParent = 0f

    init {
        context.theme.obtainStyledAttributes(
            attrs,
            R.styleable.ButtonMinWidthOfParent,
            0, 0
        ).apply {
            try {
                minWidthOfParent = getFloat(R.styleable.ButtonMinWidthOfParent_minWidthOfParent, 0f)
            } finally {
                recycle()
            }
        }
    }

    @Override
    override fun getSuggestedMinimumWidth(): Int {
        return maxOf(
            super.getSuggestedMinimumWidth(),
            ((parent as ViewGroup).measuredWidth * minWidthOfParent).toInt()
        )
    }
}
<resources>
    <declare-styleable name="ButtonMinWidthOfParent">
        <attr name="minWidthOfParent" format="float"/>
    </declare-styleable>
</resources>
<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/detail"
        android:layout_width="0dp"
        android:layout_height="100dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/secondaryButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Secondary Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/primaryButton"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/detail" />

    <com.example.secondarybutton.ButtonMinWidthOfParent
        android:id="@+id/primaryButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Primary Button that is a little bit longer"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/detail"
        app:minWidthOfParent="0.5" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/middleGuideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5" />

    <View
        android:id="@+id/view"
        android:layout_width="2dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_red_light"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>