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"的佈局)