2012-09-06 76 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

谢谢,这是一个有趣的方法来解决这个问题。 –