Отрегулируйте TextView по центру по вертикали в LinearLayout (горизонтальный) - PullRequest
2 голосов
/ 01 мая 2020

У меня четыре TextView в LinearLayout с orientation="horizontal". Ниже на рисунке я хочу, чтобы мой четвертый TextView до vertically center по отношению к другим видам. Проще говоря, я хочу немного сдвинуть свой четвертый TextView, чтобы он выглядел как в центре. Я пытался добавить макет: гравитация и поля, но он не работает.

enter image description here

Вот код:

  <LinearLayout
        android:id="@+id/tv_amenities"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_main_feature_LL"
        android:layout_marginStart="10dp"
        android:layout_marginBottom="10dp"
        android:orientation="horizontal"

        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/rating_background"
            android:drawableTop="@drawable/ic_food_and_restaurant"
            android:padding="3dp"
            android:text="Resturant"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textColor="@android:color/white" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:background="@drawable/rating_background"
            android:drawableTop="@drawable/ic_tv_black_24dp"
            android:padding="3dp"
            android:text="LCD Tv"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textColor="@android:color/white"

            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:background="@drawable/rating_background"
            android:drawableTop="@drawable/ic_local_parking_black_24dp"
            android:padding="3dp"
            android:text="Parking"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textColor="@android:color/white"

            />

        <TextView
            android:id="@+id/tv_plus_amenities"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="15dp"
            android:layout_marginBottom="20dp"
            android:background="@drawable/round_background"
            android:text="+15"

            android:textColor="@android:color/white" />


    </LinearLayout>

Ответы [ 4 ]

1 голос
/ 01 мая 2020

Я проверил ваш код внутри макета ограничения, и все в порядке. Все, что я изменил, в вашем последнем TextView:

android:layout_gravity="center"

Или

android:layout_gravity="center_vertical"

layout_gravity = center выглядит так же, как layout_gravity = center_horizontal, потому что они находятся в вертикальной линейной раскладка. В этом случае вертикальное центрирование невозможно, поэтому layout_gravity = center центрируется только по горизонтали.

Эта ссылка может помочь вам решить проблему в LinearLayout или RelativeLayout.

Вам может пригодиться эта ссылка: В чем разница между гравитацией и layout_gravity в Android?

1 голос
/ 01 мая 2020

Используйте android: layout_gravity = "center_vertical" атрибут в вашем TextView

1 голос
/ 01 мая 2020

Вы можете легко сделать это, добавив android:layout_gravity="center_vertical" и удалив нижнюю границу.

        <TextView
        android:id="@+id/tv_plus_amenities"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_gravity="center_vertical"
        android:background="@drawable/round_background"
        android:text="+15"
        android:textColor="@android:color/white" />
1 голос
/ 01 мая 2020

Добавьте эту опору к вашему @+id/tv_amenities LinearLayout

android: gravity = "center_vertical"

...