带有ListView和按钮的Android布局

Kle*_*ine 101 java layout android listview

好吧,这个特定的布局让我烦恼.并且似乎无法找到一种方法来使用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);
Run Code Online (Sandbox Code Playgroud)

小智 136

我想这就是你要找的东西.

<?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>
Run Code Online (Sandbox Code Playgroud)

  • 你也不要在`RelativeLayout`上调用`addRule(RelativeLayout.ABOVE)`.你在`RelativeLayout.LayoutParams`上调用它.然后将`ListView`添加到`RelativeLayout`,提供`RelativeLayout.LayoutParams`.请考虑切换到XML并对其进行充气,以实现长期可维护性. (7认同)
  • 这段代码应该可以正常工作 `android:layout_above ="@ id/testbutton"`将`ListView`保持在`Button`之上. (2认同)

小智 57

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

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

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


Dal*_*las 20

我只是在寻找这个问题的答案,这是最初的结果之一.我觉得好像所有答案,包括目前被选为"最佳答案"的答案都没有解决被问到的问题.正在陈述的问题是两个组件存在重叠,Button并且ListViewListView占据整个屏幕,并且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>
Run Code Online (Sandbox Code Playgroud)

注意使用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>
Run Code Online (Sandbox Code Playgroud)

只有两个不同之处.首先,我已经转而使用了LinearLayout.这将有助于下一位,这是android:layout_weight我的ListView

我希望这有帮助.


Pat*_*fka 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>
Run Code Online (Sandbox Code Playgroud)

  • 如果您正在使用(我认为1.5,甚至可能是1.6),那么在使用RelativeLayout规则时,必须在ListView之前使用按钮 - 否则,布局还不知道按钮,因为它按顺序读取它们,这就是ListView扩展的原因过去他们. (2认同)