![](/img/trans.png)
[英]RelativeLayout, align left/right for two buttons, align center for one
[英]How to align Items both left and right in same RelativeLayout
這是我為ListView的Custom Row寫的XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="#EFEFF4"
android:paddingLeft="10dp">
<ImageView
android:layout_height="20dp"
android:layout_width="20dip"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:background="@drawable/CircleRed"
android:id="@+id/statusColor" />
<TextView
android:id="@+id/tvStatusName"
android:layout_toRightOf="@id/statusColor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#696969"
android:paddingRight="20dp"
android:layout_marginLeft="10dp"
android:gravity="center_vertical|left"
android:layout_centerVertical="true"
android:text="Customer Satisfaction"
android:textSize="18dip" />
<ImageView
android:layout_alignParentRight="true"
android:layout_height="20dp"
android:layout_width="10dp"
android:layout_toRightOf="@id/tvStatusName"
android:layout_centerVertical="true"
android:src="@drawable/Right"/>
</RelativeLayout
我的ListView項的向右箭頭未正確對齊,它不在屏幕中,還是其位置取決於TextView的文本長度
但是我在Android Studio Designer中得到了這樣的信息。
謝謝通過使用向右箭頭XML對此進行了工作。
<ImageView
android:layout_alignParentRight="true"
android:layout_height="40dp"
android:layout_width="20dp"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"
android:src="@drawable/Right"/>
只需從“箭頭圖像”視圖中刪除android:layout_toRightOf="@id/tvStatusName"
並設置android:layout_toLefttOf="@id/imageViewRight."
android:layout_toRightOf="@id/tvStatusName"
android:layout_toLefttOf="@id/imageViewRight."
將imageViewRight ID賦予右箭頭圖像視圖。
Use linear layout instead of relative layout.Your code some what like this:`<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/view_margin_medium">
<TextView
android:id="@+id/lbl_driver_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/color_black"
android:text="Hari Piorko" />
<TextView
android:id="@+id/lbl_vehicle_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/lbl_driver_name"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:text="MH-08-6789" />
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_toRightOf="@+id/lbl_vehicle_no"
android:layout_weight="0">
<RadioButton
android:id="@+id/radio_driver"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:layout_gravity="right"
android:buttonTint="@color/color_green" />
</RadioGroup>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/color_gray" />
</LinearLayout>`
您是否嘗試將其安裝在設備或模擬器中是什么樣子? 右箭頭在模擬器中是否可見,請檢查一次。 如果未顯示,請嘗試以下代碼
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="#EFEFF4"
android:paddingLeft="10dp">
<ImageView
android:layout_height="20dp"
android:layout_width="20dip"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:background="@mipmap/ic_launcher"
android:id="@+id/statusColor" />
<TextView
android:id="@+id/tvStatusName"
android:layout_toRightOf="@id/statusColor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#696969"
android:paddingRight="20dp"
android:layout_marginLeft="10dp"
android:gravity="center_vertical|left"
android:layout_centerVertical="true"
android:text="Customer Satisfaction"
android:textSize="18dip" />
<ImageView
android:layout_alignParentRight="true"
android:layout_height="20dp"
android:layout_width="30dp"
android:layout_centerVertical="true"
android:padding="5dp"
android:src="@mipmap/ic_launcher" />
</RelativeLayout>
無需更改代碼。 希望對您有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.