2012-03-07 94 views
1

[編輯]避免與Android重疊:layout_centerVertical =在RelativeLayout的

「真」 鑑於:

<?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" > 

    <TextView 
     android:id="@+id/text1" 
     android:layout_width="match_parent" 
     android:layout_height="200dip" 
     android:background="#ff00ff00" /> 

    <TextView 
     android:id="@+id/text2" 
     android:layout_width="match_parent" 
     android:layout_height="200dip" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:background="#ffff0000" /> 

</RelativeLayout> 

我怎樣才能避免在小屏幕上文本1和文本之間的重疊。重要提示:

  • 如果可能的話,沒有重疊,文本2,應準確地在屏幕的垂直中心顯示

  • 是否有重疊,文本2應該向下移動了一下,text1的

    下面直接顯示

回答

1

android:gravity="center_vertical"添加到子代LinearLayout。這將使它成爲父母的中心。

更改爲button_container的android:layout_height="fill_parent"。這將使其填充父級的剩餘高度。您的代碼會是這個樣子的變化後:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#ffff0000" 
    android:text="TextView" /> 

<LinearLayout 
    android:id="@+id/button_container" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center_vertical" 
    android:orientation="vertical" > 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="test" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="test" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="test" /> 
</LinearLayout> 

+0

謝謝,但這裏的button_container是在剩餘空間的中間,而不是在屏幕的中間。我需要它在屏幕中間(如果有足夠的空間可用)。 – 2012-03-07 09:15:01

+0

你已經改變了整個問題,所有嘗試的答案現在已經脫離了語境。請把它作爲一個單獨的問題提出。 – Akhil 2012-03-07 12:36:41