2012-09-06 73 views
6

我是Android新手,所以它可能會是一個愚蠢的問題,但這裏就是這樣。Android:屏幕分辨率的百分比版面高度

我有一個活動的RelativeLayout。在這個佈局中,我在屏幕底部有另一個佈局。有時我隱藏它或使其可見,這不重要。是否有可能使另一個佈局的高度可以說總屏幕高度的27%?這個想法是保留其他內容,除了這個佈局在屏幕上。

有沒有人試過類似的東西?

+0

你的意思是說,你希望你的屏幕的兩個部分 - 頂部是屏幕的73%,第二個是27%?或者你的意思是你想要一個位於另一個視圖內的視圖覆蓋整個父視圖的底部27%。 – brianestey

回答

22

LinearLayout可以通過android:layout_weight來處理。例如,這裏是包含三個Button窗口小部件,佔用分別爲50%,30%,和高度的20%,佈局:

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

    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="50" 
     android:text="@string/fifty_percent"/> 

    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="30" 
     android:text="@string/thirty_percent"/> 

    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="20" 
     android:text="@string/twenty_percent"/> 

</LinearLayout> 

但是,不能簡單地聲明,在任意點任意插件在你的佈局文件中應該佔用屏幕大小的任意百分比。儘管如此,歡迎您使用Java執行該計算並根據需要調整窗口小部件的LayoutParams

+0

完美解決方案 –

1

你可以試試這個,更復雜但是有用。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/parent_view" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/app_name" /> 

<LinearLayout 
    android:id="@+id/parent_of_bottom_layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:layout_alignParentBottom="true" > 

    <LinearLayout 
     android:id="@+id/content_view" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="27"> 

     //Add your content here 

    </LinearLayout> 
</LinearLayout> 

+0

您可以擴展它如何工作,爲什麼它回答這個問題? –

+0

您不能在RelativeLayout中使用android:layout_weight,因此,我使用LinearLayout在相對佈局中按比例製作weignt,我從我的代碼項目中找到了解決方案,因此我分享了它。 – wSakly

+0

謝謝,這是一個有趣的方法來解決這個問題。 –