首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >带有ListView和按钮的安卓布局

带有ListView和按钮的安卓布局
EN

Stack Overflow用户
提问于 2010-03-05 08:55:06
回答 7查看 94.7K关注 0票数 101

好吧,这个特定的布局让我很恼火。而且似乎找不到一种方法来拥有一个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);
EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2010-03-05 09:24:24

我想这就是你要找的。

<?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>
票数 137
EN

Stack Overflow用户

发布于 2010-10-07 18:10:03

我有同样的问题很长时间了。

解决方法是在ListView上设置android:layout_weight="1.0“,这样可以让ListView保持在按钮上方,但又不会在列表很长时遮盖住按钮。保留按钮上的layout_weight未设置,以便它们保持其自然大小,否则按钮将被缩放。这适用于LinearLayout。

在安卓ApiDemos中有一个例子:ApiDemos/res/layout/linear_layout_9.xml

票数 57
EN

Stack Overflow用户

发布于 2010-12-05 17:31:18

我只是在寻找这个问题的答案,这是第一批结果之一。我觉得似乎所有的答案,包括目前被选为“最佳答案”的答案,都没有解决被问到的问题。所述的问题是两个组件ButtonListView之间存在重叠,因为ListView占据了整个屏幕,并且按钮在视觉上浮动在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

我希望这能帮到你。

票数 20
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2383847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档