安卓:是否可以通过导航抽屉进行点击?AppCompat v7:r21

7
当我打开我创建的安卓应用程序中的导航抽屉时,我仍然可以点击主布局(内容布局)中的列表项。我的意思是,实际上我能够通过我的导航抽屉点击一个 ListView 项。目前在导航抽屉中没有可点击的项目,但我已经将其放置在一个具有白色背景的适当 FrameLayout 中。我在一个片段中设计了抽屉的内容。这是我的代码:
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(要放入@+id/drawer_frame的DrawerFragment)

<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="siddhant.chavlekar87@gmail.com"
        android:textColor="@android:color/white" />

</RelativeLayout>

</LinearLayout>

为什么会发生这种情况?我正在使用AppCompat v7:r21库来为我的Lollipop(Material Design)应用程序提供向后兼容性。


在 Android 21 发布之前,我曾经遇到过这种问题,我通过在 DrawerLayout 中的布局中添加以下代码解决了它:android:clickable="true" - Kosh
@k0sh 所以我是把它添加到 DrawerLayout 中的所有布局还是只添加到抽屉布局中呢? - Adifyr
不,您需要将它添加到 DrawerLayout 中除了将容纳抽屉子项的布局之外的其他布局中。 - Kosh
@k0sh 嘿,我在我的 drawer_frame 中添加了这行代码,一切似乎都正常工作了。所以我猜两种方法都可以使用,对于将其添加到你的抽屉中是否存在任何危险吗? - Adifyr
1个回答

15

因此,我在我的drawer_frame FrameLayout中添加了上述提到的代码android:clickable="true",到目前为止,我还没有遇到任何困难。虽然,如果我把它放在其他布局中,这个bug仍然会出现...

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接