使用"activatedBackgroundIndicator"导航抽屉选择项目的自定义背景颜色

Fla*_*ash 12 android android-theme android-fragments

这个问题在SO上已经被问了很多,我已经引用了所有的答案.我的导航抽屉上的所选项目仍然保留默认的Holo蓝色背景.我是Java新手,我对"上下文"部分感到困惑.setAdapter().
我的项目是一个Activity,使用导航抽屉交换了多个片段.

这是我的适配器:

mDrawerListView.setAdapter(new ArrayAdapter<String>(
            // First parameter - Context
            getActionBar().getThemedContext(),
            // Second parameter - Layout for the row
            R.layout.fragment_navigation_drawer_list_item,
            // Third parameter - ID of the TextView to which the data is written
            android.R.id.text1,
            // Forth - the Array of data
            new String[]{
                    getString(R.string.title_section1),
                    getString(R.string.title_section2),
                    getString(R.string.title_section3),
                    getString(R.string.title_section4),
            }));
    mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
Run Code Online (Sandbox Code Playgroud)

这里的上下文来自Android Studio中的"预先烹饪"导航抽屉.我认为这将是所选项目的导航抽屉项目背景颜色的答案.所以我改变了我的上下文getActivity().getBaseContext(),,但这并没有改变任何东西.

我的主题(styles.xml):

<resources>
    <style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <!-- API 14 theme customizations can go here. -->
        <item name="android:actionBarStyle">@style/ActionBar</item>
    </style>

    <!-- Navigation Drawer styling -->
    <style name="NavDrawerItemSelected" parent="AppBaseTheme">
        <item name="android:activatedBackgroundIndicator">@drawable/activated_background</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

activated_background 在'drawables'目录中:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true" android:drawable="@color/green" />
    <item android:state_selected="true" android:drawable="@color/green" />
    <item android:state_pressed="true" android:drawable="@color/green" />
    <item android:state_checked="true" android:drawable="@color/green" />        
    <item android:drawable="@android:color/transparent" />
</selector>
Run Code Online (Sandbox Code Playgroud)

我不知道应该使用上述哪些状态,所以我添加了我能找到的所有状态.
最后,当选择一个项目时mDrawerListView.setItemChecked(position, true);被调用.
一切都有效,除了自定义主题样式.(最小API = 11,在API 17 AVD上测试)

tre*_*r-e 25

我遇到了这个问题.问题是背景R.layout.fragment_navigation_drawer_list_item是需要改变你的drawable.只需添加android:background="@drawable/activated_background"到布局.

  • 这是对SO问题的最简单,最清晰的答案。 (2认同)

u2t*_*all 9

我无法找到这个问题的任何完整明确的答案,所以这里是:

步骤1.指定适配器将使用的列表项布局,在此示例中,我们指定:R.layout.fragment_navigation_drawer_list_item

像这样:

mDrawerListView.setAdapter(new ArrayAdapter<String>(
        // First parameter - Context
        getActionBar().getThemedContext(),
        // Second parameter - Layout for the row
        R.layout.fragment_navigation_drawer_list_item,
        // Third parameter - ID of the TextView to which the data is written
        android.R.id.text1,
        // Forth - the Array of data
        new String[]{
                getString(R.string.title_section1),
                getString(R.string.title_section2),
                getString(R.string.title_section3),
                getString(R.string.title_section4),
        }))`
Run Code Online (Sandbox Code Playgroud)

步骤2.创建并自定义fragment_navigation_drawer_list_item.xml指定一个具有选择器的drawable,如下所示:android:background="@drawable/activated_background" 完整示例:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:background="@drawable/activated_background" />
Run Code Online (Sandbox Code Playgroud)

第3步.在问题中创建和自定义activated_background.xml文件中的选择器,它将如下所示:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true" android:drawable="@color/green" />
    <item android:state_selected="true" android:drawable="@color/green" />
    <item android:state_pressed="true" android:drawable="@color/green" />
    <item android:state_checked="true" android:drawable="@color/green" />
    <item android:drawable="@android:color/transparent" />
</selector>
Run Code Online (Sandbox Code Playgroud)