Android - выравнивать текстовое изображение в центре другого вида - программирование
Подтвердить что ты не робот

Android - выравнивать текстовое изображение в центре другого вида

У меня есть некоторые кнопки изображения, и каждый из них имеет соответствующее текстовое представление, я хотел бы выровнять их в центре их соответствующего изображения, я имею в виду, как видно на ящике приложения...

 !-----!
 !icon !
 !_____!
app  name

это мой код, я использую RelativeLayout

<ImageButton
    android:id="@+id/blog_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/logo_img"
    android:layout_marginLeft="50dp"
    android:layout_marginTop="51dp"
    android:background="@null"
    android:contentDescription="@string/blog_desc"
    android:src="@drawable/blog" />

<TextView
    android:id="@+id/blog_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/blog_button"
    android:layout_below="@+id/blog_button"
    android:layout_marginTop="8dp"
    android:text="@string/blog_desc" />
4b9b3361

Ответ 1

Оберните это в LinearLayout и центрируйте детей так:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/logo_img"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <ImageButton
        android:id="@+id/blog_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:contentDescription="@string/blog_desc"
        android:src="@drawable/blog" />

    <TextView
        android:id="@+id/blog_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/blog_desc" />
</LinearLayout>

Ответ 2

Старый пост, но если это может помочь, я думаю, что нет необходимости добавлять дополнительный контейнер.

<ImageButton
    android:id="@+id/blog_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/logo_img"
    android:layout_marginLeft="50dp"
    android:layout_marginTop="51dp"
    android:background="@null"
    android:contentDescription="@string/blog_desc"
    android:src="@drawable/blog" />

<TextView
    android:id="@+id/blog_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/blog_button"
    android:layout_alignRight="@+id/blog_button"
    android:layout_below="@+id/blog_button"
    android:gravity="center_horizontal"
    android:layout_marginTop="8dp"
    android:layout_marginLeft="-20dp"
    android:layout_marginRight="-20dp"
    android:text="@string/blog_desc" />

Это работает для меня.

Ответ 3

Если TextView может быть прозрачным, это может быть достигнуто с помощью одного View

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Test string"
    android:drawableTop="@android:drawable/btn_star"
    android:background="@android:color/transparent"
    android:textSize="10sp"
     />