listview底部的按钮不起作用

时间:2014-04-20 13:36:40

标签: android listview button layout

我有一个列表视图,并希望在我的屏幕上有两个按钮。问题是,一旦列表变得太长,按钮就会消失。我该怎么做才能让瓶子始终可见?

main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >


<ExpandableListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
</ExpandableListView>

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="do something"
        android:text="some text" />

    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="do something"
        android:text="some text" />

</LinearLayout>
</LinearLayout>

3 个答案:

答案 0 :(得分:1)

使用相对布局并在其底部放置按钮。将列表视图设置为上面的按钮linearlayout。

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


<ExpandableListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/buttons">
</ExpandableListView>

<LinearLayout android:id="@+id/buttons"
 android:layout_alignParentBottom="true"

 android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="do something"
        android:text="some text" />

    <Button

        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="do something"
        android:text="some text" />

</LinearLayout>
</RelativeLayout>

如果你想继续使用linearlayout和weight,那么为线性布局和列表设置高度为0dp:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >


<ExpandableListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
</ExpandableListView>

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="do something"
        android:text="some text" />

    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="do something"
        android:text="some text" />

</LinearLayout>
</LinearLayout>

根据需要调整重量。

答案 1 :(得分:1)

您可以创建仅包含按钮的布局文件,然后您可以使用ListView.addFooterView()将其作为ListView的页脚添加到布局中。

示例:

LinearLayout footerLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.footer,null); mListView.addFooterView(footer);

答案 2 :(得分:0)

您应该使用相对布局作为主/父布局。然后在您的孩子线性布局中,您可以设置和属性说alignparentbottom =“true”,您的可扩展列表视图可以在线性布局上方对齐,例如见下面

<RelativeLayout
//Write your other attributes here>

<ExpandableListView
//Write your other attributes here
//add one more atribute and set it to align above the linearlayout>
</ExpandableListView>

<LinearLayout
//Write your other attributes here
//add one attribute alignparent bottom and set it to "true">
</LinearLayout>

</RelativeLayout>