我注意到Google应用程序中的抽屉是可滚动的,但我不能出于某种原因得出如何实现可滚动的DrawerLayout的结论.我尝试使用以下设计范例构造布局文件.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".mainScreen">
<!-- Layout of Activity -->
</FrameLayout>
<!-- DrawerLayout segment -->
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/drawerLinearLayout"
android:orientation="vertical"
android:layout_width="260dp"
android:layout_height="match_parent"
android:layout_gravity="start|bottom"
android:layout_marginTop="?android:attr/actionBarSize"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#77000000">
<!-- Layout of Drawer -->
</LinearLayout>
</ScrollView>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
但是,无论有没有ScrollView,当抽屉超出屏幕末端时,抽屉只会将底部的物品切掉.我无法启用任何形式的滚动.不确定我缺少什么或需要启用.我们将不胜感激.
DrawerLayout段中的LinearLayout包含不同的样式视图.一个视图仅显示标题,下方有一个分隔符,一个显示带有文本旁边的图像视图,另一个显示带有内置于该行的开关的标题.因此,如果在XML编码布局文件之外完成,则需要考虑多个样式的视图.
虽然使用ListView是一个有效的选项,但对我来说,尝试和解决我已经在上面的ScrollView之间为我的布局构建了如此大的XML文件的情况非常有用.事实证明,为了使其按预期工作,只需要进行一些小的修改.构建的最新布局文件如下:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".mainScreen">
<!-- Layout of Activity -->
</FrameLayout>
<!-- DrawerLayout segment -->
<ScrollView
android:id="@+id/scrollView"
android:layout_width="260dp"
android:layout_height="match_parent"
android:layout_gravity="start|bottom"
android:layout_marginTop="?android:attr/actionBarSize">
<LinearLayout
android:id="@+id/drawerLinearLayout"
android:orientation="vertical"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#77000000">
<!-- Layout of Drawer -->
</LinearLayout>
</ScrollView>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
主要修改要求我改变重力和边缘存在的位置.重力需要在周围的ScrollView中,否则会导致在某些情况下没有滚动或实际崩溃的奇怪行为.然后需要将内部布局更改为"换行内容".
如果边距也未移入ScrollView,则显然不会向下滚动到底部.它在底部留下了未滚动的卷轴边缘.一旦解决了这个问题,DrawerLayout就像预期的那样工作.ListView选项也提出了另一种使用方法,但正如此处所提到的,值得我再分析一下,重新使用我已编写过的代码; 特别是需要在视图中处理的几个不同的自定义视图.