Android布局,包含ListView和按钮

101

好的,这个特定的布局让我很烦恼。似乎找不到一种方法使listView在底部有一排按钮,以便listview不会覆盖按钮,并且按钮始终贴着屏幕底部。这就是我想要的:

已删除失效的ImageShack链接

看起来应该很容易,但是我尝试过的所有方法都失败了。有什么帮助吗?

这是我的当前代码:

    RelativeLayout container = new RelativeLayout(this);
    container.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));

    //** Add LinearLayout with button(s)

    LinearLayout buttons = new LinearLayout(this);

    RelativeLayout.LayoutParams bottomNavParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    bottomNavParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    bottomNavParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    buttons.setLayoutParams(bottomNavParams);


    ImageButton newLayer = new ImageButton(this);
    newLayer.setImageResource(R.drawable.newlayer);
    newLayer.setLayoutParams(new LinearLayout.LayoutParams(45, LayoutParams.FILL_PARENT));
    buttons.addView(newLayer);

    container.addView(buttons);

    //** Add ListView

    layerview = new ListView(this);

    RelativeLayout.LayoutParams listParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    listParams.addRule(RelativeLayout.ABOVE, buttons.getId());

    layerview.setLayoutParams(listParams);

    container.addView(layerview);

好问题,很多开发人员会遇到这个问题。 - Signcodeindie
谢天谢地,Android中的设计师实际上非常好用。因此,在创建布局/用户界面时,请尝试使用设计师/XML。特别是对于这种情况,因为它只是一组按钮和ListView。 - Subby
7个回答

137

我认为这就是你正在寻找的内容。

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

    <Button android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:id="@+id/testbutton"
        android:text="@string/hello" android:layout_alignParentBottom="true" />

    <ListView android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:id="@+id/list"
        android:layout_alignParentTop="true" android:layout_above="@id/testbutton" />

</RelativeLayout>

1
我唯一要更改的是在ListView周围添加一个RelativeLayout,因为我无法在ListView上调用addRule(RelativeLayout.ABOVE)。 - Kleptine
2
这段代码应该可以正常工作。android:layout_above="@id/testbutton"将使ListView位于Button上方。 - CommonsWare
当我使用这个功能时,列表会停在按钮上方,然后开始内部滚动。 - lars
7
RelativeLayout上,您不会调用addRule(RelativeLayout.ABOVE),而是在RelativeLayout.LayoutParams上调用。然后,您将使用该RelativeLayout.LayoutParamsListView添加到RelativeLayout中。请考虑切换到XML并进行填充以实现长期可维护性。 - CommonsWare
我想补充一点,在布局的XML文件中,ListView 必须被放在 Button 之后(就像这个例子),否则Eclipse会不停地抱怨它找不到你在 layout_above 属性中所引用的资源。 - buc
显示剩余5条评论

57

我有同样的问题很久了。

将android:layout_weight="1.0"设置在ListView上,可以使其保持在按钮上方,但当列表很长时,避免它覆盖按钮。将按钮的layout_weight设置为空,以保持它们自然大小,否则按钮将被缩放。这适用于LinearLayout。

在Android ApiDemos中有一个示例: ApiDemos/res/layout/linear_layout_9.xml


只是为我节约几个小时。我不理解为什么按钮从未出现。 :) - frostymarvelous
我不完全明白为什么这样能运行,但它确实能运行 :) - Bevor

20

我刚刚在搜索答案时发现这个问题,它是最初的搜索结果之一。我感觉所有的答案,包括当前被选为“最佳答案”的答案,都没有解决所提出的问题。问题是两个组件ButtonListView之间有重叠,即ListView占据了整个屏幕,而Button在视觉上浮在(在前面)ListView上方(挡住了ListView中最后一个项目的视图/访问)。

根据我在这里和其他论坛上看到的答案,我最终得出了解决这个问题的结论。

最初,我的代码是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#FF394952">

  <ListView
    android:id="@+id/game_list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    />

  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    style="@android:style/ButtonBar">

    <Button
      android:id="@+id/new_game"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="@string/new_game"
      android:textColor="#FFFFFFFF"
      android:background="@drawable/button_background" />
  </LinearLayout>

</RelativeLayout>

请注意使用RelativeLayout作为根节点。

这是最终的工作版本,在此版本中,按钮不会覆盖ListView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#FF394952">

  <ListView
    android:id="@+id/game_list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_weight="1.0" />

  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    style="@android:style/ButtonBar">

    <Button
      android:id="@+id/new_game"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="@string/new_game"
      android:textColor="#FFFFFFFF"
      android:background="@drawable/button_background" />
  </LinearLayout>

</LinearLayout>

只有两个差别。首先,我切换到使用 LinearLayout。这将有助于接下来的部分,即在我的 ListView 上添加 android:layout_weight

希望这可以帮到您。


这个问题解决了,但是谁能猜到呢!当你在一个LinearLayout的父元素中使用android:layout_alignParentBottom时,ADT会报错“LinearLayout中的布局参数无效:layout_alignParentBottom”! - Aswin Kumar

8

最好的方法是使用相对布局,将按钮设置在列表视图下方。在这个例子中,按钮也位于线性布局中,因为这样更容易将它们放在同样大小的一侧。

<RelativeLayout android:id="@+id/RelativeLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent">

<ListView android:id="@+id/ListView01" 
android:layout_alignParentTop="true"
android:layout_width="fill_parent" 
android:layout_height="fill_parent">
</ListView>

<LinearLayout android:id="@+id/LinearLayout01" 
android:layout_below="@+id/ListView01" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_alignParentBottom="true">
<Button android:id="@+id/ButtonJoin" 
android:text="Join"
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignParentBottom="true">
</Button>
<Button android:id="@+id/ButtonJoin" 
android:layout_alignRight="@id/ButtonCancel" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel"
android:layout_alignParentBottom="true">
</Button>
</LinearLayout>

</RelativeLayout>

问题是,如果ListView太长,它会延伸到底部的按钮上方。但我无法弄清如何使其停在按钮上方。 - Kleptine
2
如果您使用的是1.5版本或1.6版本,则在使用RelativeLayout规则时,必须将按钮放置在ListView之前 - 否则,布局尚未意识到按钮因为它按顺序读取它们,这就是为什么您的ListView会延伸到按钮的后面。 - Tim H
这对我不起作用。像Tim H说的那样,对我来说,我必须将ListView放在LinearLayout之后,然后使ListView布局在上面,就像repoc的答案一样。 - Nemi
这不会起作用,按钮不在屏幕上,也无法滚动。 - Vaibhav Mishra

1

我知道这篇文章有点老了,但是为了回答原帖作者的问题,代码没有起作用的原因是buttons.getId()返回-1。如果你要这样做,你需要设置像调用buttons.setId(10)这样的东西。如果你这样做,代码就可以正常工作了。


0
最简单的解决方案是创建两个线性布局,一个包含按钮,另一个包含列表视图(在按钮高度上使用Wrap content,在列表布局高度上使用match parent)。然后只需在列表视图布局上添加一个滚动视图,按钮布局将被忽略。希望这可以帮到你,抱歉我不想写出代码。

-1

这应该可以工作。如果想要在ListView上方放置按钮,可以将按钮放在另一个线性布局中。

<LinearLayout> main container // vertical

<LinearLayout> scrollview must be contained in a linear layout //vertical - height to fill parent

    <ScrollView> set the height of this to fill parent

        <ListView> will be contained in the scrollview
        </ListView>

    </ScrollView>

</LinearLayout>

<LinearLayout> //horizontal - height to wrap content

<Button>

</Button>

</LinearLayout>

</LinearLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接