Android导航抽屉无法使用Scrollview

时间:2016-08-16 09:20:48

标签: android xml android-scrollview android-navigation-drawer

我正在开发一个Android应用程序,它具有带导航抽屉的scrollview布局。但导航抽屉没有扩展或不使用滚动视图。但是当我使用LinearLyout而不是ScrollView时,导航抽屉工作正常。

这是我的xml文件:

这是activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout   xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

这是app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout     xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.hotel.hotelreservation.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout>

这是content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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">

<ScrollView android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    xmlns:android="http://schemas.android.com/apk/res/android">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:src="@drawable/hotellogo"
        android:layout_marginTop="50dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Arrival Date"
        android:id="@+id/textView2"
        android:textColor="#c0392b"
        android:layout_marginTop="50dp"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="date"
        android:ems="10"
        android:id="@+id/editText"
        android:drawableLeft="@drawable/date_icon"
        android:hint="  yyyy-mm-dd"
        android:padding="20dp"
        android:textColor="#ffffff"
        style="@style/Base.V12.Widget.AppCompat.EditText"
        android:backgroundTint="#c0392b"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Departure Date"
        android:id="@+id/textView3"
        android:textColor="#c0392b"
        android:layout_marginTop="30dp"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="date"
        android:ems="10"
        android:id="@+id/editText1"
        android:drawableLeft="@drawable/date_icon"
        android:hint="  yyyy-mm-dd"
        android:padding="20dp"
        android:textColor="#ffffff"
        style="@style/Base.V12.Widget.AppCompat.EditText"
        android:backgroundTint="#c0392b"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="SEARCH"
        android:background="#c0392b"
        android:textColor="#ffffff"
        android:layout_marginTop="30dp"
        android:id="@+id/btnSearch"
        android:onClick="submit"/>

</LinearLayout>
</ScrollView>
</android.support.v4.widget.DrawerLayout>

这是nav_header_mail.xml

<?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="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    android:src="@android:drawable/sym_def_app_icon" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    android:text="Hotel Booking"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="An online hotel booking system" />

</LinearLayout>

3 个答案:

答案 0 :(得分:0)

我使用了你的布局并对其进行了修改和工作检查我附上了我的截图

enter image description here

以下是代码

<?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:orientation="vertical">


    <ScrollView android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        xmlns:android="http://schemas.android.com/apk/res/android">


        <ImageView
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:src="@drawable/hotellogo"
            android:layout_marginTop="50dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Arrival Date"
            android:id="@+id/textView2"
            android:textColor="#c0392b"
            android:layout_marginTop="50dp"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="date"
            android:ems="10"
            android:id="@+id/editText"
            android:drawableLeft="@drawable/date_icon"
            android:hint="  yyyy-mm-dd"
            android:padding="20dp"
            android:textColor="#ffffff"
            style="@style/Base.V12.Widget.AppCompat.EditText"
            android:backgroundTint="#c0392b"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Departure Date"
            android:id="@+id/textView3"
            android:textColor="#c0392b"
            android:layout_marginTop="30dp"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="date"
            android:ems="10"
            android:id="@+id/editText1"
            android:drawableLeft="@drawable/date_icon"
            android:hint="  yyyy-mm-dd"
            android:padding="20dp"
            android:textColor="#ffffff"
            style="@style/Base.V12.Widget.AppCompat.EditText"
            android:backgroundTint="#c0392b"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="SEARCH"
            android:background="#c0392b"
            android:textColor="#ffffff"
            android:layout_marginTop="30dp"
            android:id="@+id/btnSearch"
            android:onClick="submit"/>

    </LinearLayout>
    </ScrollView> 
    </LinearLayout>
    <!-- The navigation drawer ListView-->
        <ListView android:id="@+id/left_drawer_home"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@null"
            android:dividerHeight="0dp"
           android:background="#ffffff"/>


    </android.support.v4.widget.DrawerLayout>

答案 1 :(得分:0)

使用NestedScrollView而不是ScrollView

   <android.support.v4.widget.NestedScrollView
    android:id="@+id/nScrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <FrameLayout ...>
       <ListView ... />
    </FrameLayout >
</android.support.v4.widget.NestedScrollView>  

答案 2 :(得分:0)

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/drawer_layout"
        tools:context="com.ft.parse.sms.HomeActivity">

        <ScrollView android:id="@+id/scroll"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/main_container"
                android:background="@color/colorDarkYellow">

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="250dp"
                    android:layout_marginTop="10dp"
                    android:id="@+id/fr_home_one">

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_below="@id/fr_home_one"
                    android:id="@+id/fr_home_two">

                </RelativeLayout>
            </RelativeLayout>
        </ScrollView>

        <android.support.design.widget.NavigationView
            app:headerLayout="@layout/header"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@color/colorWhite"
            app:itemTextColor="@color/colorDarkGrey"
            app:itemIconTint="@color/colorDarkGrey"
            android:id="@+id/navigation_view"
            app:menu="@menu/drawer_menu"
            android:layout_gravity="start"
            >
        </android.support.design.widget.NavigationView>

    </android.support.v4.widget.DrawerLayout>


Above code works well with 2 fragments attached in a activity with scrollable view