2014-03-24 115 views
1

我想在top和bottom按钮之间添加一个listView。我不希望它有一个固定的大小,所以我试图把它像这样:在两个元素之间添加listView

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

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

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

但设置布局文件时,这种方式,列表视图当然是第一按钮下方的,但去从那里到屏幕的按钮,而不停留在按钮2的上方。 - > listview与button2重叠。

我该如何放置这两个按钮之间的列表视图?

+0

这样的事情? http://blog.maxaller.name/2010/05/attaching-a-sticky-headerfooter-to-an-android-listview/ – sherpya

回答

1

附加android:layout_above="@+id/button2"属性为ListView如下...这将保持ListView高于button2

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

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

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

这个解决方案非常好,但是如果你真的不需要外部RelativeLayout的其他任何LinearLayout版本的高度为0蚂蚁重量是更有效率。 –

1

类似的东西应该做的工作

<LinearLayout android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical> 
    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="0px" 
     android:weight="1"/> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
    </LinearLayout> 
1

试试这个..

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

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

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

<Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 
</LinearLayout> 

或者

<RelativeLayout 
     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:layout_alignParentTop="true" /> 

<ListView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_above="@+id/button2" 
    android:layout_below="@+id/button1" /> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" /> 
</RelativeLayout> 
0

试试这个方法:

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

    <Button 
     android:id="@+id/button1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:text="Button" /> 

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

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

</RelativeLayout> 

这完美的作品按您的要求。这很简单... :)

0

您可以使用RelativeLayoutListView您可以通过参数layout_abovelayout_below设置位置。

0

尝试将您的xml插入到RelativeLayout中。

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

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

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

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

</RelativeLayout> 
0

将此属性设置为您的ListView

<ListView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_below="@+id/button1" 
    android:layout_above="@+id/button2" /> 
相关问题