The app:layout_constraintVertical_chainStyle="spread_inside" or any of the options – packed, spread – has not effect; why?

huangapple go评论61阅读模式
英文:

The app:layout_constraintVertical_chainStyle="spread_inside" or any of the options - packed, spread - has not effect; why?

问题

在提供的代码中,在父级ConstraintLayout上设置了属性app:layout_constraintVertical_chainStyle="spread_inside"。该属性控制垂直链中视图之间的间距行为。

根据文档,"spread_inside"链样式应该均匀分布链中视图之间的额外空间,同时将第一个和最后一个视图与链的顶部和底部对齐。

然而,在渲染布局时,链样式似乎没有任何效果。这可能是由于其他约束与链样式发生冲突。要使它生效,您可以采取以下措施:

  1. 检查其他约束:确保没有其他约束与垂直链上的视图发生冲突。可能存在其他约束,例如对单个视图的约束,可能会影响链样式的表现。

  2. 验证视图的约束:确保每个视图在垂直链中都设置了正确的约束。这包括起始和结束约束,以及与相邻视图的顶部和底部约束。

  3. 检查链条中视图的高度:如果视图的高度不是"wrap_content",而是固定值,这也可能会影响链样式。确保视图的高度允许链样式正常工作。

通过仔细检查约束和确保链中的视图设置正确的属性,您应该能够使"spread_inside"链样式正常工作。

英文:
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintVertical_chainStyle="spread_inside">

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/button2"
        app:layout_constraintTop_toTopOf="parent"/>

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/button1"
        app:layout_constraintBottom_toTopOf="@id/button3"/>

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/button2"/>


</androidx.constraintlayout.widget.ConstraintLayout>

In the provided code, the attribute app:layout_constraintVertical_chainStyle="spread_inside" is set on the parent ConstraintLayout. This attribute controls the spacing behavior between views in a vertical chain.

According to the documentation, the "spread_inside" chain style should evenly distribute the extra space between views in the chain, while aligning the first and last views to the top and bottom of the chain, respectively.

However, when the layout is rendered, the chain style does not appear to have any effect. This could be due to other constraints that are conflicting with the chain style; what can I do to make it to work?.

答案1

得分: 0

根据文档的说明:链条由设置在链条第一个元素上的属性控制,所以不要将属性应用于ConstraintLayout,而是应用于链条中最顶部的小部件:

<androidx.appcompat.widget.AppCompatButton
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/button2"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="spread_inside" />
英文:

According to the documentation: Chains are controlled by attributes set on the first element of the chain, so instead of applying the attribute to the ConstraintLayout, apply it to the top most widget inside the chain:

&lt;androidx.appcompat.widget.AppCompatButton
        android:id=&quot;@+id/button1&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;Button 1&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintBottom_toTopOf=&quot;@+id/button2&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;
        app:layout_constraintVertical_chainStyle=&quot;spread_inside&quot; /&gt;

huangapple
  • 本文由 发表于 2023年2月20日 00:05:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定