![](/img/trans.png)
[英]Are layout_alignParentRight or layout_alignParentLeft work for include layout?
[英]layout_centerInParent overrides layout_alignParentRight/layout_alignParentLeft in Android 4.1.2
我正在嘗試將RelativeLayout中的三個視圖對齊,以使所有三個視圖垂直居中對齊。 然后,我希望視圖“ A”在RelativeLayout中向左對齊,視圖“ C”在右邊,以及視圖“ B”在“ A”和“ C”之間。 XML聲明如下所示:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ViewA
android:id="@+id/viewA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
... />
<ViewC
android:id="@+id/viewC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
... />
<ViewB
android:id="@+id/viewB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/viewC"
android:layout_toRightOf="@+id/viewA"
... />
</RelativeLayout>
在Android 4.2.2及更高版本上,但在低於此版本(SDK 16)上,這可以正常工作。 似乎android:layout_centerInParent會覆蓋android:layout_alignParentLeft / android:layout_alignParentRight,因為“ A”和“ C”都出現在布局的中間,而不是分別向左和向右對齊。 有沒有一種方法可以在RelativeLayout內部使用其他方式設置垂直對齊方式?
當您想使其垂直居中時,為什么要使用android:layout_centerInParent="true"
。 請改用android:layout_centerVertical="true"
。
嘗試這種方式,希望這將幫助您解決問題。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textViewA"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20sp"
android:text="A"/>
<TextView
android:id="@+id/textViewB"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20sp"
android:text="B"/>
<TextView
android:id="@+id/textViewC"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20sp"
android:text="C"/>
</LinearLayout>
使您的布局像這樣
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical" >
<View
android:id="@+id/viewA"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:background="#ff00" />
<View
android:id="@+id/viewC"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentRight="true"
android:background="#00ff00" />
<View
android:id="@+id/viewB"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerHorizontal="true"
android:background="#0000ff" />
</RelativeLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.