我需要具有两个视图的Android布局。第一个视图是TextView
,第二个视图是ImageView
。 ImageView
应始终与TextView
的右侧对齐。 TextView
应该能够扩展以填充任何剩余的空间,具体取决于文本的大小。如果文本太大,则ImageView
绝不能被TextView
隐藏。在这种情况下,文本应被截尾。
这是我要完成的工作的视觉效果:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom | center_vertical">
<TextView
android:id="@+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:ellipsize="end"
android:singleLine="true"/>
<ImageView
android:id="@+id/myImage"
android:layout_width="12dp"
android:layout_height="12dp"
android:src="@drawable/myImageDrawable"
android:layout_toRightOf="@id/myText"/>
</RelativeLayout>
以上XML不起作用,因为TextView
的文本太大时会隐藏ImageView
。如何解决此代码?我也愿意使用其他布局。请注意,TextView
必须为一行。
下面的xml足以实现您想要的。只需将singleLine
设置为true
,然后使用drawableEnd
设置图像即可。另外,用您的代码替换我代码中的文本。仅此而已。
<TextView
android:singleLine="true"
android:text=" HehhsasasashasgghgahgshagshgahsghagshaghsgahsghaHehhsasasashasgghgahgshagshgahsghagshaghsgahsgha shgasagsasghag shahsghag"
android:layout_width="wrap_content"
android:drawableEnd="@drawable/myImageDrawable"
android:layout_height="wrap_content"/>