【发布时间】:2014-10-29 17:47:53
【问题描述】:
我的应用中有一个文本视图,其中 android:layout_width 设置为 fill_parent,android:gravity 为“中心”。现在我想在textview的文本旁边有drawable。这可能吗?
【问题讨论】:
我的应用中有一个文本视图,其中 android:layout_width 设置为 fill_parent,android:gravity 为“中心”。现在我想在textview的文本旁边有drawable。这可能吗?
【问题讨论】:
使用这些来设置文本视图旁边的可绘制对象,具体取决于所需的位置。
【讨论】:
单独使用TextView 无法实现您想要的效果。
一种选择是使用自定义布局。您可以查看HorizontalTailLayout。很容易在你的项目中获取和投入,这应该可以工作。
另一个不完美的选择是使用RelativeLayout 和android:gravity="fill"。这将确保ImageView 始终紧贴TextView 的尾部。但缺点是即使使用其他布局包裹也无法使内容居中,因为使用android:layout_alignParentRight="true" 会将RelativeLayout 一直拉伸到右侧。
例子:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="fill">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_alignParentRight="true"
android:src="@android:drawable/ic_lock_lock"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/iv"
android:layout_centerVertical="true"
android:singleLine="true"
android:ellipsize="end"
android:text="Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! "/>
</RelativeLayout>
【讨论】: