android - TextView 宽度匹配drawableTop 宽度

标签 android textview android-drawable xml-drawable

有什么方法可以使TextView宽度匹配复合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----] |
|-----------------|

最佳答案

您可以通过将图像和 TextView 分离到相对布局中来简单地使其工作。它可以轻松指定对齐某些 View 边缘的位置等。

下面的代码应该做一些接近你想要的事情:

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

关于android - TextView 宽度匹配drawableTop 宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17620723/

相关文章:

html - 从 XML 中的 html 格式的字符串资源设置 TextView 文本

java - Jsoup显示数据到textview

android - Android drawables 文件夹中的图像存储位置?

android - 显示 fragment 点击

android - 如何布局按钮阵列以适应任何屏幕尺寸

java - 我无法配置个人适配器

android - 每个特定字符的不同颜色

java - Android 比较背景颜色和颜色

android - 使用可绘制对象 xml 文件和图像

android - 如何从android中优雅的数字按钮获取固定值?