TextView width match drawableTop width

可紊 提交于 2019-12-24 04:56:06

问题


Is there someway to make TextView width match compound Drawable width (XML)?

For example for xml code:

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

I get:

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

But what i really want is:

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

回答1:


You can simply get it to work by separating the image and the TextView, into a relative layout. It makes easy to specify where align some view's edges, etc.

The following code should do something close to what you want:

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


来源:https://stackoverflow.com/questions/17620723/textview-width-match-drawabletop-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!