DrawerLayout内部布局的选择器不起作用

时间:2014-08-08 20:31:25

标签: android navigation-drawer

我的视图与导航抽屉的底部对齐,不在列表中。

activity_main.xml中     

<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">

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

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/loading_layout"
            android:visibility="invisible">
                <ProgressBar
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/progressBar"
                    android:layout_centerInParent="true" />

        </RelativeLayout>
    </FrameLayout>

    <RelativeLayout
        android:id="@+id/navigation_drawer_content"
        android:layout_width="@dimen/navigation_width"
        android:layout_height="match_parent"
        android:background="@color/navdrw_bg"
        android:clickable="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_gravity="start" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/navigation_support_item_height"
            android:id="@+id/support_project"
            android:focusableInTouchMode="true"
            android:clickable="true"
            android:focusable="true"
            android:background="@drawable/support_project_selector"
            android:layout_alignParentBottom="true">

            <ImageView
                android:layout_width="@dimen/navigation_item_icon_size"
                android:layout_height="@dimen/navigation_item_icon_size"
                android:id="@+id/navigation_icon"
                android:layout_marginLeft="@dimen/navigation_item_icon_left_margin"
                android:layout_marginRight="@dimen/navigation_item_icon_right_margin"
                android:layout_centerVertical="true"
                android:src="@drawable/ic_menu_support"
                android:layout_alignParentLeft="true"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/navdrw_bg"
                android:text="@string/navigation_support_project"
                android:textSize="@dimen/navigation_item_text_size"
                android:id="@+id/navigation_name"
                android:layout_centerVertical="true"
                android:gravity="center_vertical"
                android:layout_toRightOf="@+id/navigation_icon"/>

        </RelativeLayout>

        <ListView android:id="@+id/left_drawer"
            android:layout_width="@dimen/navigation_width"
            android:layout_height="match_parent"
            android:layout_above="@+id/support_project"
            android:layout_gravity="left"
            android:dividerHeight="0dp"
            android:divider="@null"/>

    </RelativeLayout>

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

是id,支持id_project。问题是这种布局的选择器不起作用。

support_project_selector.xml

<?xml version="1.0" encoding="utf-8"?>
    <selector
        xmlns:android="http://schemas.android.com/apk/res/android">

        <!-- Selected -->
        <item
            android:drawable="@color/navdrw_support_block_bg_pressed"
            android:state_focused="true"
            android:state_selected="true"/>

        <!-- Pressed -->
        <item
            android:drawable="@color/navdrw_support_block_bg_pressed"
            android:state_focused="false"
            android:state_selected="true"/>

        <!-- When not selected -->
        <item
            android:drawable="@color/navdrw_support_block_bg"/>

    </selector>

我尝试了很多变种而没有任何帮助。选择器在未单击视图时安装颜色,但单击它时不会更改颜色。

但是布局是可点击的,我确实收到点击事件并且可以处理它。问题仅在于点击/激活颜色。

选择器中用于活动和正常状态的颜色是100%完全不同。

1 个答案:

答案 0 :(得分:1)

在这里,您处理的方面有很多不太好的方面。

首先,您的选择器可能指出:

每个要自定义的状态应该有item个标记,每个项目标记应该有一个不同的android:state值。因此,从两个状态(已选中和已按下)中删除android:state_focused属性。

pressed州,当您使用android:state_selected="true"时,您正在使用android:state_pressed="true"。这将使您的选择器在实际按下项目时工作,而不仅仅是在选中它时。

其次,为了影响RelativeLayout的选择器,您必须通过在xml布局中添加android:clickable=true属性来将其设置为可点击。默认情况下,RelativeLayout不可点击,例如,与Button不同。

最后,将android:duplicateParentState="true"添加到您的RelativeLayoutViewsImageViewTextView。此属性允许子项具有与父项相同的状态。当您选择或按下RelativeLayout时,该子状态将复制该状态。

你完成了。你的选择器正在工作。