2012-01-01 116 views
2

我想添加一个listview或tableview占据2/3的屏幕,然后会有一个巨大的按钮在listview下方的中心。现在的问题是,listview占据屏幕的整个高度。我无法调整图形布局上的高度。我想只占用5个项目的高度尺寸。下面将屏幕上的按钮中心,Android布局在相关布局下的Listview高度调整

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/relativeLayout" 
    android:padding="3dp" xmlns:android="http://schemas.android.com/apk/res/android"> 
<ListView android:layout_height="wrap_content" 
    android:id="@+id/listView1" 
    android:layout_width="fill_parent" android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true"></ListView> 

</RelativeLayout> 

回答

3

从你在你的问题说,这听起来像一个垂直的LinearLayout会更好地为您。 通过这种方式,您可以通过在顶层LinearLayout中放置两个视图,使ListView占据屏幕的三分之二,并使用权重在屏幕上分配视图。

例如:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 
    <ListView 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="2" > 
    </ListView> 
    <RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" > 
     <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true"/> 
    </RelativeLayout> 
</LinearLayout> 
+0

了android:layout_alignParentBottom属性可能会导致隐藏在按钮下方的一些列表项。 – mvsagar 2014-07-23 14:50:02

4
<?xml version="1.0" encoding="utf-8"?> 

    <RelativeLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/relativeLayout" 
     android:padding="3dp" xmlns:android="http://schemas.android.com/apk/res/android"> 

<ListView android:layout_height="fill_parent" 
     android:id="@+id/listView1" 
     android:layout_above="@+id/button1" 
     android:layout_width="fill_parent"></ListView> 

<Button android:layout_height="wrap_content" 
     android:id="@+id/button1" 
     android:layout_alignParentBottom="true" 
     android:layout_width="fill_parent"></Button > 

    </RelativeLayout> 
+0

+1这种方式,仍然保持使用RelativeLayout – superjos 2012-03-22 09:11:11

+0

这对我有效。谢谢。不过,除了android:layout_above之外,我必须使用属性android:layout_below来确保列表视图位于应用程序中的其他两个视图之间。 – mvsagar 2014-07-23 14:47:45