6

当我在我创建的android应用程序中打开我的导航抽屉时,我仍然可以在我的主布局(内容布局)中单击列表项目。我的意思是,我实际上可以点击ListView项目我的导航抽屉。我在导航抽屉中没有任何可点击的项目,但我已将它放在一个带有白色背景的适当FrameLayout中。抽屉的内容,我正在设计一个片段。这里是我的代码:Android:能够点击导航抽屉? AppCompat v7:r21

activity_home.xml(MainActivity)

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/white" 
    tools:context=".HomeActivity"> 

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

    <include 
     android:id="@+id/toolbar_home" 
     layout="@layout/my_awesome_toolbar" /> 

    <FrameLayout 
     android:id="@+id/main_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <FrameLayout 
      android:id="@+id/content_frame" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

     <ImageButton 
      android:id="@+id/fab_addContact" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="end|bottom" 
      android:layout_marginBottom="@dimen/view_margin_large" 
      android:layout_marginEnd="@dimen/view_margin_large" 
      android:layout_marginRight="@dimen/view_margin_large" 
      android:background="@drawable/fab_button" 
      android:contentDescription="@string/fab_contDesc" 
      android:padding="@dimen/view_margin_large" 
      android:src="@drawable/ic_add_white_24dp" /> 

    </FrameLayout> 

</LinearLayout> 

<FrameLayout 
    android:id="@+id/drawer_frame" 
    android:layout_width="300dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:background="@android:color/white" 
    android:clickable="true" /> 

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

fragment_drawer.xml(DrawerFragment进入了@+id/drawer_frame

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/white" 
    android:orientation="vertical"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="144dp"> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scaleType="centerCrop" 
     android:src="@drawable/material_design_wallpaper" /> 

    <ImageView 
     android:id="@+id/iv_userThumbnail" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" 
     android:layout_margin="16dp" 
     android:background="@drawable/display_pic_thumbnail" 
     android:contentDescription="@string/contact_thumbnail" /> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/view_margin_xx_large" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:background="@drawable/shadow" /> 

    <TextView 
     android:id="@+id/tv_userName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/tv_userEmail" 
     android:layout_alignLeft="@+id/tv_userEmail" 
     android:layout_alignStart="@+id/tv_userEmail" 
     android:layout_marginBottom="@dimen/view_margin_small" 
     android:text="Siddhant Chavlekar" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="@android:color/white" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/tv_userEmail" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginBottom="@dimen/view_margin_large" 
     android:layout_marginLeft="@dimen/view_margin_large" 
     android:layout_marginStart="@dimen/view_margin_large" 
     android:text="[email protected]" 
     android:textColor="@android:color/white" /> 

</RelativeLayout> 

</LinearLayout> 

这究竟是为什么?我正在使用AppCompat v7:r21库为我的棒棒糖(Material Design)应用程序提供向后兼容性。

+0

我甚至在发布android 21之前就已经有了这种体验,我已经通过将此行添加到我的布局DrawerLayout'android:clickable =“true”' – k0sh 2014-11-09 05:25:47

+0

@ k0sh将它添加到我的'DrawerLayout'里面的所有布局还是只放在抽屉里? – SynerFlow 2014-11-09 08:35:19

+0

不,你把它添加到drawerlayout内部的布局,除了布局,它将持有抽屉孩子 – k0sh 2014-11-09 08:36:28

回答

13

所以,我把上面提到的那行代码是android:clickable="true"在我的drawer_frame FrameLayout中,到目前为止我还没碰到过任何困难。虽然,如果我把它放在我的其他布局,错误仍然出现...

所以我想解决方案是把android:clickable="true"在代表抽屉的布局。 (与android:layout_gravity="start"的布局)