【问题标题】:How do you align a button to the bottom of a ListView that sticks to the bottom even after deleting a row?即使在删除一行后,如何将按钮与仍然粘在底部的 ListView 的底部对齐?
【发布时间】:2014-08-20 10:08:29
【问题描述】:

我正在尝试创建一个布局,其中包含 ListView,其下方有一个按钮,并且还使按钮“粘”到 ListView 的底部,即使我在其中添加或删除一行列表视图。

使用下面的布局代码,当我向列表添加新行时,按钮“移动”到 ListView 底部正下方的正确位置。所以效果很好。

问题是当我从 ListView 中删除一行时,按钮会停留在原位并且不会“向上移动”,因此它会粘在 ListView 的底部。当我旋转设备并重新创建视图时,按钮实际上会向上移动,但我希望它在删除一行时自动向上移动。

这是我现在的代码:

<LinearLayout   
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >                

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

    <ImageButton
        android:id="@+id/addButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/add_button" />

</LinearLayout>

解决方案:

解决方案是创建一个仅包含按钮的小布局文件,然后以编程方式将其添加为 ListView 的页脚。

在我的片段中:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) 
{ 

    ...

    View footerView = inflater.inflate(R.layout.listview_footer, null, false);

    addButton = (ImageButton) footerView.findViewById(R.id.addButton);

    listView.addFooterView(footerView);

    data = getListViewData();

    adapter = new MyListAdapter(getActivity(), data);

    listView.setAdapter(adapter);

    ...

}

listview_footer.xml:

<?xml version="1.0" encoding="utf-8"?>

<ImageButton
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/addButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/add_button" />

listview_footer.xml 只包含按钮,没有布局。

现在,当我从ListView 中删除一行时,按钮会向上移动以“粘”到 ListView 的底部。和以前一样,当我向 ListView 添加一行时,按钮会向下移动。

【问题讨论】:

  • 尝试将此按钮添加为页脚。
  • 或使用相对布局并使用对齐父底部

标签: android android-listview android-button


【解决方案1】:

您可以在列表视图的页脚添加按钮。

View footerView = View.inflate(this, R.layout.footer, null);
listview.addFooterView(footerView);

为页脚视图创建一个 xml 文件,您将根据 UI 需要在其中添加一个按钮。

Footer.xml 可能如下所示:

<?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"
 android:padding="5dip">

 <Button android:id="@+id/previous"
  android:layout_width="wrap_content" android:layout_height="wrap_content"
  android:layout_centerHorizontal="true" />

</RelativeLayout>

This link可能对你有帮助。

【讨论】:

    【解决方案2】:
    <RelativeLayout   
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         >                
    
        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
             />           
    
        <ImageButton
            android:id="@+id/addButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below= "@+id/listView"
            android:src="@drawable/add_button" />
    
    </RelativeLayout>
    

    【讨论】:

    • 由于循环依赖(ListView 上方的按钮和 Button 下方的 ListView),这不会编译。
    • @JDJ 你检查了吗?
    • 这使得 ListView 再次出现,但是当我删除一行时按钮不会向上移动。
    • 尝试将listview的高度改为wrap_content
    【解决方案3】:

    我有类似的情况,我希望一个按钮始终位于列表的右下角,并且列表中会有很多删除/添加。我使用了相对布局:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/search_bg" >
    
        <ListView
            android:id="@+id/list_searchfilters"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
           android:divider="@android:color/transparent"
            android:dividerHeight="10dp"
            android:listSelector="@drawable/search_list_selector" />
    
        <Button
            android:id="@+id/add_filter_button"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginTop="20dp"
            android:layout_below="@id/list_searchfilters"
            android:layout_alignParentRight="true"
            android:layout_marginRight="15dp" />
    </RelativeLayout>
    

    每次删除或添加一行时,我都会调用LayoutUtils.setListViewHeightBasedOnChildren(mSearchListView);,我将其定义为:

    public static void setListViewHeightBasedOnChildren(ListView listView) {
                ListAdapter listAdapter = listView.getAdapter(); 
                if (listAdapter == null) {
                    // pre-condition
                    return;
                }
    
                int totalHeight = 0;
                for (int i = 0; i < listAdapter.getCount(); i++) {
                    View listItem = listAdapter.getView(i, null, listView);
                    listItem.measure(0, 0);
                    totalHeight += listItem.getMeasuredHeight();
                }
    
                ViewGroup.LayoutParams params = listView.getLayoutParams();
                params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
                listView.setLayoutParams(params);
                listView.requestLayout();
            }
    

    【讨论】:

    • 谢谢。这看起来是一个有趣的解决方案。我得试试看。
    【解决方案4】:

    改为用户RelativeLayout。将android:layout_alignParentBottom="true" 添加到图像按钮,将android:layout_above="@+id/addButton" 添加到列表视图

        <RelativeLayout   
            android:layout_width="match_parent"
            android:layout_height="match_parent"
             >                
    
            <ListView
                android:id="@+id/listView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
        android:layout_above="@+id/addButton"
     android:layout_alignParentTop="true"/>           
    
            <ImageButton
                android:id="@+id/addButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/add_button" 
                android:layout_alignParentBottom="true"
    
        />
    
        </RelativeLayout>
    

    【讨论】:

    • android:layout_weight 在 RelativeLayout 中不起作用。
    • 由于某种原因,ListView 在这种布局下是不可见的。
    • 由于某种原因它仍然不可见。
    • ListView 不可见,因为高度设置为 0dp
    猜你喜欢
    • 1970-01-01
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 1970-01-01
    相关资源
    最近更新 更多