Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

android constraintlayout - How to define a Flow virtual layout which vertically stacks and horizontally aligns its referenced views?

I have defined a ConstraintLayout which contains a Flow virtual layout which tightly wraps and vertically stacks three views, as follows:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/view1"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:background="@android:color/black" />

    <View
        android:id="@+id/view2"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:background="@android:color/black" />

    <View
        android:id="@+id/view3"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@android:color/black" />

    <androidx.constraintlayout.helper.widget.Flow
        android:id="@+id/flow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:constraint_referenced_ids="view1,view2,view3"
        app:flow_horizontalAlign="start"
        app:flow_horizontalStyle="packed"
        app:flow_maxElementsWrap="1"
        app:flow_verticalAlign="top"
        app:flow_verticalGap="8dp"
        app:flow_verticalStyle="packed"
        app:flow_wrapMode="chain"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Despite the flow_horizontalAlign value of start, this renders the views as horizontally centred, as follows:

enter image description here

Does anyone know how I can start/left align the referenced views instead?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Adding app:flow_horizontalBias="0" to your Flow widget will align the views to start.

You can also set android:orientation="vertical" for the Flow widget, use the default mode none in the app:flow_wrapMode and then flow_horizontalAlign="start" should work as expected.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...