约束流百分比无法正常工作

时间:2019-11-08 16:22:00

标签: android android-layout android-constraintlayout

我正在使用constraintlayout version 2.0.0-beta2

约束流百分比无法正常工作。我正在使用约束流助手构建链。我正在尝试应用基于百分比的约束,但未按预期工作。水平排列3个框,并应用百分比约束

布局XML
  

<LinearLayout 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="wrap_content"
    android:orientation="vertical">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context=".MainActivity">

        <androidx.constraintlayout.helper.widget.Flow
            android:id="@+id/flow"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:constraint_referenced_ids="textView1,textView2,textView3"
            app:flow_horizontalStyle="packed"
            app:flow_maxElementsWrap="3"
            app:flow_verticalAlign="center"
            app:flow_verticalStyle="packed"
            app:flow_wrapMode="chain"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:ignore="MissingConstraints" />

        <LinearLayout
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_blue_bright"
            android:orientation="vertical"
            app:layout_constraintWidth_percent="0.4"
            tools:ignore="MissingConstraints">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView1 Tex TextView1 Tex TextView1 Tex TextView1 TexTextView1 TexTextView1 Tex TextView1 Tex" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_red_dark"
            android:orientation="vertical"
            app:layout_constraintWidth_percent="0.4"
            tools:ignore="MissingConstraints">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView1 Tex TextView1 Tex TextView1 Tex TextView1 TexTextView1 TexTextView1 Tex TextView1 Tex" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/textView3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_orange_light"
            android:orientation="vertical"
            app:layout_constraintWidth_percent="0.2"
            tools:ignore="MissingConstraints">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView1 Tex TextView1 Tex TextView1 Tex TextView1 TexTextView1 TexTextView1 Tex TextView1 Tex" />
        </LinearLayout>


    </androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>

屏幕截图

1 个答案:

答案 0 :(得分:0)

检查以下内容是否可以解决您的问题,

<LinearLayout 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"
android:orientation="vertical">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="20dp"
    tools:context=".MainActivity">

    <androidx.constraintlayout.helper.widget.Flow
        android:id="@+id/flow"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        app:constraint_referenced_ids="textView1,textView2,textView3"
        app:flow_horizontalStyle="packed"
        app:flow_maxElementsWrap="3"
        app:flow_verticalAlign="center"
        app:flow_verticalStyle="spread"
        app:flow_wrapMode="chain"
        app:flow_horizontalGap="20dp"
        app:flow_verticalGap="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints" />

    <LinearLayout
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@android:color/holo_blue_bright"
        android:orientation="vertical"
        app:layout_constraintEnd_toStartOf="@+id/textView2"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView1 Tex TextView1 Tex TextView1 Tex TextView1 TexTextView1 TexTextView1 Tex TextView1 Tex" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@android:color/holo_red_dark"
        android:orientation="vertical"
        app:layout_constraintEnd_toStartOf="@+id/textView3"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/textView1"
        tools:ignore="MissingConstraints">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView1 Tex TextView1 Tex TextView1 Tex TextView1 TexTextView1 TexTextView1 Tex TextView1 Tex" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/textView3"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@android:color/holo_orange_light"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/textView2"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView1 Tex TextView1 Tex TextView1 Tex TextView1 TexTextView1 TexTextView1 Tex TextView1 Tex" />
    </LinearLayout>


</androidx.constraintlayout.widget.ConstraintLayout>