androidxmlandroid-flexboxlayout

How center "TextView" in flexbox?


I try to center text in a FlexboxLayout. I use this lib com.google.android:flexbox:0.2.5. I have several text in column. I try many method to put them in midle of that column without success.

<com.google.android.flexbox.FlexboxLayout
            xmlns:app="http://schemas.android.com/apk/res-auto"
            style="?metaButtonBarStyle"
            android:layout_width="77dp"
            android:layout_height="match_parent"
            android:background="@drawable/bg_pattern_dark_bitmap"
            android:orientation="vertical"
            android:alpha="1"
            app:flexWrap="wrap"
            app:justifyContent="space_between"
            >

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="lol"
                />
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="lol"
                />
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="lol"
                />
    </com.google.android.flexbox.FlexboxLayout>

Thank you for any help


Solution

  • Add this line in TextView

     android:gravity="center_vertical"
    

    For imageview

    put imageview inside Linearlayout and set gravity

      <com.google.android.flexbox.FlexboxLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        style="?metaButtonBarStyle"
        android:layout_width="77dp"
        android:layout_height="match_parent"
        android:background="@drawable/bg_pattern_dark_bitmap"
        android:orientation="vertical"
        android:alpha="1"
        app:flexWrap="wrap"
        app:justifyContent="space_between">
    
    <LinearLayout android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:gravity="center_vertical">
    
        <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:background="@color/colorPrimary"/>
    
    </LinearLayout>
    
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="lol"
            android:gravity="center_vertical"/>
    
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="lol"
            android:gravity="center_vertical"/>
    
    </com.google.android.flexbox.FlexboxLayout>