【问题标题】:Android Layout with ListView and Buttons带有 ListView 和按钮的 Android 布局
【发布时间】:2011-01-23 22:33:48
【问题描述】:

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

【问题讨论】:

  • 问得好,很多开发者都会卡在这个问题上。
  • 谢天谢地,Android 中的设计师其实很可爱。因此,在创建布局/UI 时,请尝试使用 Designer/XML。特别是对于这种情况,因为它只是一组按钮和一个 ListView。

标签: java android listview layout


【解决方案1】:

我多年来一直有同样的问题。

将ListView保持在按钮上方,但防止在列表较长时将其覆盖的解决方案是在ListView上设置android:layout_weight="1.0"。保留未设置按钮上的 layout_weight 以便它们保持自然大小,否则按钮将被缩放。这适用于 LinearLayout。

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

【讨论】:

  • 只是为我节省了几个小时。无法理解为什么按钮从未出现。 :)
  • 我不完全理解为什么这样有效,但它有效 :)
【解决方案2】:

我只是在寻找这个问题的答案,这是最初的结果之一。我觉得好像所有的答案,包括目前被选为“最佳答案”的答案都没有解决被问到的问题。正在说明的问题是 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 作为根节点。

这是最终的工作版本,其中 Button 不与 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。这将有助于下一点,即将android:layout_weight 添加到我的ListView

我希望这会有所帮助。

【讨论】:

  • 这解决了,但谁能猜到!?当您在 LinearLayout 父级中提供 android:layout_alignParentBottom 时,ADT 会显示“LinearLayout 中的布局参数无效:layout_alignParentBottom”!
【解决方案3】:

最简单的解决方案是创建两个线性布局,一个带有按钮,另一个带有列表视图(在按钮高度上包装内容并在列表布局高度上匹配父级)。然后仅使用列表视图在布局上创建滚动视图,按钮布局将被忽略。希望对你有帮助,对不起,我不想写代码。

【讨论】:

    【解决方案4】:

    这应该可以。要在列表视图上方也有按钮,请将按钮放在另一个线性布局中。

    <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>
    

    【讨论】:

      【解决方案5】:

      我知道这篇文章相当陈旧,但是,为了回答原始发帖人的问题,代码不起作用的原因是buttons.getId() 返回-1。如果你要这样做,你需要设置调用buttons.setId(10)之类的东西。如果你这样做,代码就可以正常工作。

      【讨论】:

        【解决方案6】:

        我想这就是你要找的。​​p>

        <?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>
        

        【讨论】:

        • 这就是我基本上一直在使用的,尽管我正在用 Java 编写布局。 listView 仍然延伸到按钮之上。
        • 我唯一要改变的是我在 ListView 周围添加了一个 relativeLayout,因为我不能在 ListView 上调用 addRule(RelativeLayout.ABOVE)。
        • 这段代码应该可以正常工作。 android:layout_above="@id/testbutton" 将保持ListView 高于Button
        • 当我使用它时,我让列表停止在按钮上方,然后开始在内部滚动。
        • 您也不会在 RelativeLayout 上调用 addRule(RelativeLayout.ABOVE)。你打电话给RelativeLayout.LayoutParams。然后将ListView 添加到RelativeLayout,提供RelativeLayout.LayoutParams。请考虑切换到 XML 并对其进行扩展,以实现长期可维护性。
        【解决方案7】:

        最好的方法是设置列表视图下方的按钮的相对布局。在此示例中,按钮也是线性布局,因为它们更容易以相同的大小并排放置。

        <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>
        

        【讨论】:

        • 问题在于,如果列表视图太长,它会延伸到底部按钮的顶部。我不知道如何让它停在按钮上方。
        • 如果您使用的是(我认为是 1.5,甚至可能是 1.6),在使用 RelativeLayout 规则时,您必须在 ListView 之前拥有按钮 - 否则,布局还不知道按钮,因为它会将它们读入顺序,这就是您的 ListView 超出它们的原因。
        • 这对我不起作用。就像 Tim H 说的那样,对我来说,我必须将 ListView 放在 LinearLayout 之后,然后制作 ListView layout_above,就像上面 repo 的回答一样。
        • 这不起作用,按钮不在屏幕上,也不会滚动
        猜你喜欢
        • 2013-08-06
        • 1970-01-01
        • 1970-01-01
        • 2013-12-25
        • 2011-02-26
        • 1970-01-01
        • 1970-01-01
        • 2018-06-19
        • 1970-01-01
        相关资源
        最近更新 更多