![](/img/trans.png)
[英]Image view not centered in parent though layout_centerHorizontal is set to true
[英]layout_centerHorizontal item is not centered when another view is at the right side of it
當我使用layout_centerHorizontal
將一個項目在視圖中水平居中,而另一個項目在它的右側時,則未居中。
但是,當我刪除第二項時,它會移到中間。
這是代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
android:gravity="center_horizontal">
<TextView
android:id="@+id/message"
android:layout_width="240dp"
android:layout_height="240dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:background="#13D372"/>
<TextView
android:id="@+id/message2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginTop="50dp"
android:layout_toRightOf="@id/message"
android:background="#125632"
/>
</RelativeLayout>
</LinearLayout>
使用android:gravity =“ center_horizontal”將孩子(兩個TextView)放置在其容器的水平中心。
從父級移除android:gravity="center_horizontal"
,並在子級布局( TextView
)中將android:layout_centerHorizontal="true"
保留為僅居中。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6">
<TextView
android:id="@+id/message"
android:layout_width="240dp"
android:layout_height="240dp"
android:layout_centerHorizontal="true" <!--here-->
android:layout_marginTop="50dp"
android:background="#13D372"/>
<TextView
android:id="@+id/message2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginTop="50dp"
android:layout_toRightOf="@id/message"
android:background="#125632"
/>
</RelativeLayout>
</LinearLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.