Ширина TextView соответствует ширине drawableTop

Есть ли способ заставить TextView width соответствовать составной ширине Drawable (XML)?

Например, для кода xml:

<TextView
    android:id="@+id/textView"
    style="@style/TextIcons"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableTop="@drawable/icon_contact"
    android:gravity="center"
    android:lines="2"
    android:text="@string/contact_label" />

Я получил:

|---------------------|
|   |-------------|   |
|   |             |   |
|   |  Drawable   |   |
|   |    View     |   |
|   |             |   |
|   |-------------|   |
|[---Contact Label---]|
|---------------------|

Но на самом деле я хочу:

|-----------------|
| |-------------| |
| |             | |
| |  Drawable   | |
| |    View     | |
| |             | |
| |-------------| |
| [---Contact---] |
| [----Label----] |
|-----------------|

person GLopez    schedule 12.07.2013    source источник


Ответы (1)


Вы можете просто заставить его работать, разделив изображение и TextView на относительный макет. Это позволяет легко указать, где выровнять края некоторых видов и т. д.

Следующий код должен делать что-то близкое к тому, что вы хотите:

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

    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon_contact"
            android:id="@+id/imageView"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp">

    </ImageView>
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/contact_label"
            android:id="@+id/textView"
            android:layout_below="@+id/imageView"
            android:layout_alignRight="@+id/imageView"
            android:layout_alignLeft="@+id/imageView"/>
</RelativeLayout>
person jujujuijk    schedule 12.07.2013
comment
Это работает, но на самом деле я выровнял две строки по левому краю, требуется центр. - person GLopez; 12.07.2013
comment
Я понял, только добавьте android:gravity=center в TextView, спасибо. Я бы предпочел что-то попроще, но внешне нет. - person GLopez; 12.07.2013