有什么方法可以使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/