Flexbox中的“ TextView”如何居中?

时间:2019-09-30 13:45:51

标签: android xml android-flexboxlayout

我尝试在FlexboxLayout中居中放置文本。我使用这个库com.google.android:flexbox:0.2.5。 我在栏中有几个文字。我尝试了很多方法将它们放在该列的中间,但没有成功。

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

谢谢您的帮助

2 个答案:

答案 0 :(得分:0)

使用app:justifyContent="space_evenly"代替space_between

答案 1 :(得分:0)

在TextView中添加此行

 android:gravity="center_vertical"

用于图像查看

将imageview放入Linearlayout并设置重力

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