简体   繁体   English

带有ListView和按钮的Android布局

[英]Android Layout with ListView and Buttons

Alright, this specific layout is just annoying me. 好吧,这个特定的布局让我烦恼。 And can't seem to find a way to have a listView, with a row of buttons at the bottom so that the listview doesn't extend over top of the buttons, and so the buttons are always snapped to the bottom of the screen. 并且似乎无法找到一种方法来使用listView,底部有一排按钮,因此listview不会延伸到按钮的顶部,因此按钮总是被捕捉到屏幕的底部。 Here's what I want: 这就是我想要的:

removed dead ImageShack link 删除了死的ImageShack链接

It seems like it should be so easy, but everything I've tried has failed. 看起来它应该很容易,但我尝试过的一切都失败了。 Any help? 有帮助吗?

Here's my current code: 这是我目前的代码:

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

I think this is what you are looking for. 我想这就是你要找的东西。

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

I had the same problem for ages. 我多年来一直有同样的问题。

The solution to keeping the ListView above the buttons, but preventing it from covering them up when the list is long, is to set android:layout_weight="1.0" on the ListView. 将ListView保持在按钮上方但阻止它在列表很长时覆盖它们的解决方案是在ListView上设置android:layout_weight =“1.0”。 Leave the layout_weight on the buttons unset so that they remain at their natural size, otherwise the buttons will get scaled. 将layout_weight保留在​​未设置的按钮上,以使它们保持自然大小,否则按钮将缩放。 This works with LinearLayout. 这适用于LinearLayout。

There's an example in the Android ApiDemos: ApiDemos/res/layout/linear_layout_9.xml Android ApiDemos中有一个例子: ApiDemos / res / layout / linear_layout_9.xml

I was just searching for an answer to this question and this was one of the first results. 我只是在寻找这个问题的答案,这是最初的结果之一。 I feel as if all of the answers, including the one that is currently chosen as the "best answer" is not addressing the issue being asked about. 我觉得好像所有的答案,包括目前被选为“最佳答案”的答案都没有解决被问到的问题。 The problem that is being stated is that there is an overlap of the two components Button and ListView in that the ListView is taking up the entire screen, and the Button is visually floating above (in front of) the ListView (blocking view/access of the last item in the ListView ) 正在陈述的问题是ButtonListView这两个组件有重叠,因为ListView占据了整个屏幕,而Button在视觉上漂浮在ListView的上方(前面)(阻塞视图/访问ListView的最后一项)

Based on the answers I've seen here and on other forums, I finally came to a conclusion on how to resolve this. 基于我在这里和其他论坛上看到的答案,我终于得出了如何解决这个问题的结论。

Originally, I had: 最初,我有:

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

Note the use of RelativeLayout as the root node. 请注意使用RelativeLayout作为根节点。

This is the final, working version in which the Button does not overlap the ListView : 这是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>

There are only two differences. 只有两个不同之处。 First, I've switched to using a LinearLayout . 首先,我已经切换到使用LinearLayout This will help with the next bit, which was adding android:layout_weight to my ListView 这将有助于下一位,即将android:layout_weight添加到我的ListView

I hope this helps. 我希望这有帮助。

The best way is a relative layout that sets the buttons below the listview. 最好的方法是设置列表视图下方按钮的相对布局。 In this example the buttons are also in a linear layout because it is easier to put them side by side at an equal size. 在这个例子中,按钮也是线性布局,因为它们更容易并排放置在相同的尺寸。

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

I know this post is rather old, but, to answer the original poster's question, the reason the code did not work was buttons.getId() returns -1. 我知道这篇文章相当陈旧,但是,为了回答原始海报的问题,代码不起作用的原因是buttons.getId()返回-1。 If you are going to do this, you need to set do something like call buttons.setId(10). 如果你打算这样做,你需要设置像调用buttons.setId(10)这样的东西。 If you do that, the code works just fine. 如果你这样做,代码工作得很好。

this should work. 这应该工作。 to have buttons above the listview too, put the buttons inside another linear layout. 在列表视图上方也有按钮,将按钮放在另一个线性布局中。

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

the easiest solution would be to create two linear layouts, one with the button and the other with the list view(Wrap content on the button height and match parent on the list layout height). 最简单的解决方案是创建两个线性布局,一个使用按钮,另一个使用列表视图(在按钮高度上包装内容并在列表布局高度上匹配父级)。 then only make a scroll view over the layout with the list view and the button layout will be ignored. 然后只使用列表视图在布局上滚动视图,按钮布局将被忽略。 hope it helps, sorry i didn't feel like writing out the code. 希望它有所帮助,抱歉我不想写出代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM