2013-01-22 30 views
1

我想把一个ListView放在我的活动的顶部,而底部的东西没有ListView“吞下”底部的小部件?我试过这个(“别的东西”是一个RelativeLayout,只有一个按钮),但它不起作用。如何在Android中定位ListView?

<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:orientation="vertical" > 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" > 

    </ListView> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_gravity="bottom" > 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:text="Button" /> 

    </RelativeLayout> 

</LinearLayout> 

我找了一些提示here,这是非常有用的,但并没有说明ListView什么事。我可以毫无问题地将零件放在ListView以上,但不能低于。

回答

5

使用的RelativeLayout作为根布局,然后用layout_above定位ListView和按钮与layout_alignParentBottom

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@+id/button" > 

    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:text="Button" /> 

</RelativeLayout> 
+0

现在列表并没有吞下按钮,但如果列表比可用空间短,它将直接显示在按钮上方而不是顶部。是否有可能使列表出现在顶部,但仍不能吞下按钮? – petersohn

+1

@petersohn - 将ListView的高度设置为match_parent – dymmeh

0

我敢肯定,这已经回答了SO几次......

改变你这样的布局:

<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:orientation="vertical" > 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:layout_gravity="bottom" > 

    </ListView> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" > 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:text="Button" /> 

    </RelativeLayout> 

</LinearLayout> 
+0

这一个根本不显示列表。 – petersohn

+0

而且是不必要的嵌套视图 – dymmeh

+0

我刚刚从你的代码中复制了RelativeLayout,我以为你会用任何你可能有更多东西的东西来替换那个部分......这就是为什么我保留它 – Matthieu

相关问题