2012-11-15 37 views
1

我想在另一個全屏幕相對佈局中有一個相對佈局,佔用全部寬度但是其父高度的50%,最好是用XML而不是Java代碼完成。如何讓幾個RelativeLayouts佔據屏幕的一個藥水?

我已經想出瞭如何對齊父母的中心,以及如何填充寬度,但有沒有辦法獲得父母的身高的50%?什麼約30%? 6.2834%?

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

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="??????????" 
     android:layout_centerInParent="true" > 

我想做百分比的原因是,如果我用「浸」指定,而對象將保持相同的大小,佈局看起來在不同屏幕尺寸有很大不同(如手機和平板電腦)。

編輯:

感謝所有有關使用的LinearLayout和加權的答案。我也曾看過這個。我覺得我可能會過度簡化問題。說我需要的是這樣的:

enter image description here

我想我可以用複雜的LinearLayout和加權勾勒中心廣場,然後將中心廣場fill_parent,像這樣:

enter image description here

但是我應該怎樣處理其他3個方格(佈局)?我可以在另一個正方形上使用另一個LinearLayout「圖層」嗎?或者我應該把整個屏幕分成許多很多小單元,並且這些子單元跨越多個單元(不確定這是否可能)?

回答

2

我通常用的LinearLayout這個去和重量設定一定的比例:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <View 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="25"/> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="50"> 
    </RelativeLayout> 

    <View 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="25"/> 

</LinearLayout> 

您的編輯:

在有一點你需要確定佈局。首先進行分組佈局。尋找模式。在你簡單的解釋中,我們設計了一種使用線性佈局的方法將3個對象與一箇中間的對象組合在一起。隨着新的佈局,你能以任何方式分組這些項目嗎?

一旦您設置了簡單的佈局模式,可能會通過定義權重來添加您正在查找的特定間距。然後,您可能需要添加相對佈局並開始將視圖錨定到特定視圖。問問自己他們是否重疊?一個視圖總是位於其他視圖頂部還是側面。什麼定義了你的視圖的界限,然後使用線性佈局,權重,相對佈局,toLeftOf,toRightOf,波紋管,上方,邊距和填充從那裏採用它。

下面是一個例子,我的意思是像對象一樣分組。這絕不是最好的解決方案,但這一切取決於您如何定義定位參數。

黃色=垂直線性佈局

綠色=水平線性佈局

您有1個大的垂直佈局和內側的裏面多個對象兩個水平佈局。從那裏你可以把它分解成更容易管理的部分,如何安排和在該佈局內的項目。現在使用相對佈局,您可以將項目相對於另一個對象進行定位,您可以刪除由線性佈局處理的一些工作,但是您將定義相對於其他對象的距離,並且可能不得不調整以適當調整佈局在不同的屏幕尺寸(原因不使用靜態定位)。

layouts

1

也許嘗試使用LinearLayout 3佈局內與android:layout_weight設置爲1,2,1

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <RelativeLayout 
     android:background="#FF0000" 
     android:layout_weight="1" 
     android:layout_width="match_parent" 
     android:layout_height="0dip" > 
    </RelativeLayout> 

    <RelativeLayout 
     android:background="#00FF00" 
     android:layout_weight="2" 
     android:layout_width="match_parent" 
     android:layout_height="0dip" > 
    </RelativeLayout> 

    <RelativeLayout 
     android:background="#FF0000" 
     android:layout_weight="1" 
     android:layout_width="match_parent" 
     android:layout_height="0dip" > 
    </RelativeLayout> 

</LinearLayout> 
0

RelativeLayout不支持的寬度和高度爲兒童的百分比。使用LinearLayoutandroid:layout_weight屬性。

6

嘗試使用LinearLayoutweightSum

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

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#FF0000"> 

    </RelativeLayout> 

</LinearLayout> 
+0

感謝分享了android:weightSum屬性!對我來說是新的。 –

+0

是啊,不知道重量總和,更好的解決方案。 –

+0

這絕對是迄今爲止最好的解決方案,感謝分享!你能幫我解決我編輯過的問題嗎? – user1032613

3

如果你不絕對需要它嵌套在一個RelativeLayout的,你可以在LinearLayout中使用重量其他人指出。我只是在上面和下面添加了一個RelativeLayout,所以如果你正在嘗試使用屏幕的其餘部分。如果沒有,只要刪除其他RelativeLayouts。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/ParentLinearLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:weightSum="10" > 

<RelativeLayout 
    android:id="@+id/RelativeLayoutTop" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="2.5" 
    android:background="@color/torange" > 
</RelativeLayout> 

<RelativeLayout 
    android:id="@+id/RelativeLayoutMid" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="5" 
    android:background="@color/tpurple" 
    android:padding="8dp" > 

    <TextView 
     android:id="@+id/description" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:text="@string/describe" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

</RelativeLayout> 

<RelativeLayout 
    android:id="@+id/RelativeLayoutBottom" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="2.5" 
    android:background="@color/torange" > 
</RelativeLayout> 

相關問題