【发布时间】:2017-12-04 05:54:35
【问题描述】:
我正在尝试做一些我从一开始就知道很难实现的事情,那就是在屏幕底部为导航抽屉菜单放置一个页脚。
事实是,当抽屉的列表视图项在屏幕上全部可见时,我需要页脚正好位于屏幕底部,并且当元素离开屏幕时页脚应位于最后一项的下方并出现滚动条(正常行为)。
为此,我将在下一个方式中使用 addFooterView 方法
ViewGroup footer = (ViewGroup)inflater.inflate(R.layout.testme_drawer_footer, mDrawerList, false);
mDrawerList.addFooterView(footer, null, false);
其中 testme_drawer_footer 是下一个布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/footer_menu_facebook"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start"
android:orientation="horizontal">
<TextView
android:id="@+id/drawer_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#8d3169"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textColor="#fff"
android:textSize="12sp"/>
</LinearLayout>
不做任何事情,addFooterView 只是表现正常,如果元素在屏幕上都是可见的,并且底部有很多空白空间,页脚就放在最后一个元素的下方(对我想要实现的目标不利)。
我在不同的 StackOverflow 帖子中尝试了许多建议但无济于事,经过一段时间的努力,我能够得到非常接近我需要的东西,这是下一个:
我已经给所有列表视图元素一个固定的高度和相同的标题,所以最后我用 screenHeight - statusBarHeight - actionBarHeight -drawerHeaderHeight - listViewElementHeight * numberOfElements 计算页脚高度:
ViewGroup footer = (ViewGroup)inflater.inflate(R.layout.testme_drawer_footer, mDrawerList, false);
int screenHeight = GeneralUtils.getScreenHeight(oActivity);
int statusBarHeight = GeneralUtils.getStatusBarHeight(oActivity);
int actionBarHeight = GeneralUtils.getActionBarHeight(oActivity);
int drawerHeaderHeight = GeneralUtils.dp2px(60, oActivity);
int menuItemHeight = drawerHeaderHeight;
int totalItemsHeight = menuItemHeight*(endItem-startItem+1);
int footerHeight = screenHeight - statusBarHeight - actionBarHeight - drawerHeaderHeight - totalItemsHeight;
footer.setMinimumHeight(footerHeight);
mDrawerList.setFooterDividersEnabled(true);
mDrawerList.addFooterView(footer, null, false);
但很可能某些高度测量方法并不十分精确,并且在所有测试设备中存在一些像素差异,它并不相等。
我知道这不是最好的方法,事实上我不喜欢它,我不喜欢为 wrap_content 的抽屉标题和元素设置固定高度,我不喜欢整体计算高度这种方式,但找不到任何其他工作方式来实现这一点。
有什么帮助吗?
提前致谢!
这是我在所有活动中设置 ListView 的方式:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llMainMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="horizontal">
//MAIN CONTENT
<ListView
android:id="@+id/left_drawer"
android:layout_width="@dimen/navigation_drawer_width_phone"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:headerDividersEnabled="true"
android:background="#ffeeeeee"/>
</android.support.v4.widget.DrawerLayout>
【问题讨论】:
-
感谢您的回复和时间@seon,我终于解决了我的问题,如下所述。
标签: android listview footer drawer