Выровнять ImageView и TextView внутри LinearLayout

У меня есть LinearLayout и внутри, ImageView и TextView. Я хочу выровнять центр LinearLayout ImageView и TextView под ImageView.

Я получаю только верхнее выравнивание ImageView в LinearLayout. Как я могу сделать?

       <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:layout_margin="8dp"
            android:gravity="center_horizontal"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/boton"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:layout_gravity="center"
                android:background="@drawable/plano"
                android:gravity="center" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Plano"
                android:textSize="23sp"
                android:textStyle="bold"
                />

        </LinearLayout>

2 ответа

Решение

Я покажу вам, как сделать то же самое, используя преимущества RelativeLayout и componentDrawable TextView:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:layout_margin="8dp"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:drawableTop="@drawable/plano"
        android:drawablePadding="8dp"
        android:text="Plano"
        android:textSize="23sp"
        android:textStyle="bold"
    />
</RelativeLayout>

Составные чертежи помогают сократить количество просмотров и, следовательно, существенно ускоряют макет

Ключ android:drawableTop="@drawable/plano" Это заменяет верхнее изображение.
Вы также можете установить изображение справа, слева и снизу.
Даже все вместе.

Вы должны поместить поле layout_gravity как center_horizontal, но два виджета (ImageView, TextView).

Вот код:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:layout_gravity="center_horizontal"
      android:src="@drawable/ic_launcher"/>
  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textAppearance="?android:attr/textAppearanceSmall"
      android:text="Small Text"
      android:id="@+id/textView" 
      android:layout_gravity="center_horizontal"/>

</LinearLayout>
Другие вопросы по тегам