Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.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 约束以相同宽度在内部显示展开视图_Android_Android Constraintlayout_Constraint Layout Chains - Fatal编程技术网

Android 约束以相同宽度在内部显示展开视图

Android 约束以相同宽度在内部显示展开视图,android,android-constraintlayout,constraint-layout-chains,Android,Android Constraintlayout,Constraint Layout Chains,我希望四个按钮在以下条件下水平对齐: 他们的文本是25%,50%,75%,100%,所以最后一个会更宽 它们应该有相同的宽度 他们应该在剩下的空间里散布这些信息 是否可以使用ConstraintLayout实现这一点 要演示我到底想要什么,请查看我的布局的当前状态,该状态正常: 当前,按钮的宽度设置为父按钮的25%,并且使用LinearLayout实现它们之间的静态边距。有什么问题?在小屏幕中,边距可能导致最后一个按钮像下图一样被截断,或者在大屏幕中,按钮可能太大 因此,除了最后一个按钮的宽度

我希望四个按钮在以下条件下水平对齐:

他们的文本是25%,50%,75%,100%,所以最后一个会更宽 它们应该有相同的宽度 他们应该在剩下的空间里散布这些信息 是否可以使用ConstraintLayout实现这一点

要演示我到底想要什么,请查看我的布局的当前状态,该状态正常:

当前,按钮的宽度设置为父按钮的25%,并且使用LinearLayout实现它们之间的静态边距。有什么问题?在小屏幕中,边距可能导致最后一个按钮像下图一样被截断,或者在大屏幕中,按钮可能太大


因此,除了最后一个按钮的宽度外,我还希望ConstraintLayout的扩展行为是wrap\u content,其他按钮的宽度设置为与最后一个按钮相同。

因此,我想到了:

<?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">

    <Button
        android:id="@+id/tv_25"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginLeft="16dp"
        android:text="25%"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/tv_50"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/tv_50"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginLeft="16dp"
        android:text="50%"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintLeft_toRightOf="@id/tv_25"
        app:layout_constraintRight_toLeftOf="@id/tv_75"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/tv_75"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginLeft="16dp"
        android:text="75%"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintLeft_toRightOf="@id/tv_50"
        app:layout_constraintRight_toLeftOf="@id/tv_100"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/tv_100"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:text="100%"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintLeft_toRightOf="@id/tv_75"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
说明:我使用了app:layout\u constraintHorizontal\u chainStyle=spread.Using chain在均匀分布视图时考虑了边距

使用链式排列可能会使您获得预期的布局


感谢@cheticamp及其提供的答案,实现所需行为的最佳方法是编写一个简单的约束器,将所有子对象的宽度设置为最大

类MaxWidthConstraint@JVM重载构造函数 上下文:上下文,属性:属性集?=null,defStyleAttr:Int=0 :CONSTRAINTELPERCONTEXT、ATTR、defStyleAttr{ 覆盖fun updatePostMeasurecontainer:ConstraintLayout{ val最大宽度= ReferenceIDs.asSequence.map{container.getViewByIdit} .map{container.getViewWidgetit} .map{it.width} .max?:0 ReferenceIDs.asSequence.map{container.getViewByIdit} .map{container.getViewWidgetit} .forEach{it.width=maxWidth} } } 所以布局代码是:


如果要在linearlayout中使用相同的,请在xdpi、xhdpi、xhdpi下的dimen.xml中指定维度,xxhdpi,并具有特定于类别的大小,它不会在不同的devices@NehaRathore是的,这可能是一种方法,但我更喜欢一种更具动态性的方法,可以利用wrap_内容,这是一种解决堆栈溢出问题的方法。密切关注ConstraintLayout的新版本,以防有正式解决方案。@Cheticamp谢谢。这似乎是实现这一目标的最干净的方法。如果你发布了你的解决方案,我可以将它设置为答案。谢谢你的提议,但它已经是答案了。谢谢你的回答。但它仍然存在相同的问题,静态16dp的保证金。当没有足够的空间时,我想减少或消失边距。这需要一些java编码。您必须测量屏幕宽度和视图宽度,然后相应地设置边距。我会看看是否可以试试。是的,但我一直在寻找一种方法,如果XML和约束布局存在的话。如果不是,我应该在运行时将其他按钮的宽度设置为上一个按钮的宽度。在XML中不能使边距动态。链本身不会使它们的宽度相等。如果你去掉按钮上的填充物,你会发现它们是不相等的。@MohammadOmidvar:你说的是哪种填充物?我是指按钮的默认填充物。
<?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">

    <Button
        android:id="@+id/btn_25"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/entercode_default"
        android:text="25%"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/btn_50"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_50"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/entercode_default"
        android:text="50%"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/btn_75"
        app:layout_constraintStart_toEndOf="@+id/btn_25"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_75"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/entercode_default"
        android:text="75%"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/btn_100"
        app:layout_constraintStart_toEndOf="@+id/btn_50"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_100"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/entercode_default"
        android:text="100%"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/btn_75"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>