![](/img/trans.png)
[英]How to set the align parent right attribute of TextViews in relative layout programmatically
[英]How to set the width of a layout in android that extends to a point relative to the right border?
我有一個listview,左邊有一個小而長的textview(兩者都有不同的文本),而最右邊是imageview。 問題是,當長文本視圖變得太長時,它將在包裝到第二行之前壓縮右邊的圖像視圖。 由於兩個textview的每一行都有不同的長度,因此我無法為其長度設置一個固定的dp值。 我怎么辦imageview沒有被壓縮?
PS:我像這樣在listview中創建行:
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:layout_gravity="center_vertical"/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_gravity="center_vertical"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/image"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentRight="true"
android:focusable="false"/>
</RelativeLayout>
將android:toLeftOf =“ @ + id / image”添加到您的TextViews。 您可能需要將TextViews的layout_width設置為fill_parent。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="10sp"
android:toLeftOf="@+id/image"
android:layout_gravity="center_vertical"/>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:toLeftOf="@+id/image"
android:layout_gravity="center_vertical"/>
<ImageView
android:id="@+id/image"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentRight="true"
android:focusable="false"/>
</RelativeLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.