![](/img/trans.png)
[英]RelativeLayout, align left/right for two buttons, align center for one
[英]Align left edge to center - RelativeLayout
我有以下要求(大大簡化):
文本2必須從屏幕的中心開始。
我只能用LinearLayout來實現這個目的:
<more_code></more_code><LinearLayout
android:baselineAligned="false"
android:weightSum="2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_weight="1"
android:orientation="horizontal"
android:layout_width="0dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test one"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test two"/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:orientation="horizontal"
android:layout_width="0dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test three"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test four"/>
</LinearLayout>
</LinearLayout><more_code></more_code>
因為我已經有太多的嵌套視圖(因此獲取myfile.xml有10個級別,對性能警告不好 )我想知道我是否可以使用一個RelativeLayout獲得相同的結果。 我一直在閱讀文檔,但我找不到允許我這樣做的屬性。
如果你接受0dp 0dp的空視圖作為開銷,這很容易。 在我看來,這比擁有太多嵌套視圖更好,但是這可以作為其他視圖的參考。
使用Space
(0x0 px)。 如果您將此Space
水平居中,則可以將其用作參考,如下所示:
<RelativeLayout>
<!-- Empty layout (0x0 dp) centered horizontally -->
<Space android:id="@+id/dummy"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true"
android:visibility="invisible"/>
<!-- Align to parent left -->
<TextView
android:id="@+id/test_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="test one"/>
<!-- to right of the previous one, and to left of the dummy -->
<TextView
android:id="@+id/test_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/test_one"
android:layout_toLeftOf="@+id/dummy"
android:text="test two"/>
<!-- Align to right of the dummy -->
<TextView
android:id="@+id/test_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/dummy"
android:text="test three"/>
<!-- Align to right of the previous one, and parent right -->
<TextView
android:id="@+id/test_four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/test_three"
android:layout_alignParentRight="true"
android:text="test four"/>
</RelativeLayout>
你可以使用alignParentLeft =“true”將它與父左對齊
嘗試使用以下代碼(未經測試):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left side text-->
<TextView
android:id = "@+id/lefttextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text1"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"/>
<!-- center -->
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/lefttextview"
android:text="text2"/>
</RelativeLayout>
無法理解確切的問題。 你想要的是來自中心的text2,所以首先使用相對布局,因為它更容易在相對布局中進行放置,而不是線性布局,它將控件一個接一個地放置,只定位了方向。 並且您可以將屬性center_in_parent用於true,然后將text1對齊到其左側。 我希望這能解決你的問題。
您可能必須使用具有android:layout_centerHorizontal="true"
的虛擬View
和具有id的android:layout_width="0dp"
。 然后添加帶有android:layout_toRightOf="@id/id_of_dummy_view"
屬性的TextView
來定位它。 可能有一種更簡單的方法可以做到這一點,但我現在無法測試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.