Android中ListView底部的小部件

时间:2015-04-27 09:38:44

标签: android android-layout

我想在屏幕上看到的是一个列表,下面是一个按钮,但按钮应该在列表的正下方,并且始终可见,无论列表的大小如何,并且无论列表中是否有滚动条,它对于屏幕来说太大了。列表应位于屏幕顶部。我现在拥有的是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:choiceMode="singleChoice"
    android:layout_above="@+id/btn"/>
  <Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="button"
    android:layout_alignParentBottom="true"/>
</RelativeLayout>

如果列表很大(因此它需要的屏幕空间大于它),那么它会得到一个滚动条,一切都很好。我不喜欢的是当列表很短时会发生什么:在屏幕的顶部有一些列表元素,然后在底部有按钮。如果我将列表的layout_height切换到wrap_content并调整更多参数,那么按钮将位于列表的正下方,正如我想要的那样。但是使用此布局,如果列表很大,则按钮将不可见:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:choiceMode="singleChoice"
    android:layout_alignParentTop="true"/>
  <Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/list"
    android:text="button"/>
</RelativeLayout>

所以我想知道是否有办法在XML中实现“按钮靠近列表”。我倾向于认为这是不可能的,但我对Android布局没有很强的把握。转到Java,你可以计算所有类型的东西并调整布局,但这似乎容易出错,所以我最终可能会把按钮放在右边。

6 个答案:

答案 0 :(得分:1)

试试这个

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

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_weight="1.0"
    android:choiceMode="singleChoice" />

<Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/list"
    android:layout_gravity="right"
    android:text="button" />

</LinearLayout>

答案 1 :(得分:0)

作为@阿萨德的建议,有一个线性布局,并将其分为10个部分。第一部分标题,列表视图的2-9部分和按钮的最后第10部分。

答案 2 :(得分:0)

将列表设置为layout_alignParentTop AND layout_above按钮 和按钮,将其设置为layout_alignParentBottom。

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

    <ListView
        android:layout_above="@+id/btn"
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:choiceMode="singleChoice"
        android:layout_alignParentTop="true" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="button" />
</RelativeLayout>

enter image description here

答案 3 :(得分:0)

创建一个包含按钮的页脚视图布局,然后添加

g
希望它有所帮助!

答案 4 :(得分:0)

您可以使用footerView将此主xml更改为仅包含listview

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:choiceMode="singleChoice" />

</RelativeLayout>

现在创建另一个布局footer_button.xml,如下所示

<?xml version="1.0" encoding="utf-8"?>
    <Button xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_world" >
</Button>

现在在您的活动代码中

ListView lv = (ListView) findViewById(R.id.list);
    View b1 = ((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_button, null, false);
    lv.addFooterView(b1);

警告:在将适配器设置为列表视图之前添加页脚视图

答案 5 :(得分:0)

看看这个

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="48dp"
        android:choiceMode="singleChoice" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:layout_alignBottom="@id/list"
        android:text="button" />
</RelativeLayout>

请注意,按钮的固定高度设置为ListView的填充。对于这两个维度,您应该从 dimen.xml 引用相同的值。