[英]Android: How to align an ImageView to the right of a TextView and make sure the ImageView is always visible
我需要一個帶有兩個視圖的 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"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.