使用“activatedBackgroundIndicator”导航抽屉自定义选定项的背景颜色

12
这个问题在SO上被问了很多次,我参考了所有的答案。但是在我的导航抽屉中,选定项仍然使用默认的Holo蓝色背景。我是Java的新手,对于.setAdapter()中的"context"部分感到困惑。
我的项目是一个单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);

这里的上下文来自于Android Studio中的“预制”导航抽屉。我认为这可能是答案Navigation Drawer item background colour for selected item。因此,我将我的上下文更改为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>

'drawables'文件夹中的activated_background

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

我不知道应该使用上面列出的哪个状态,所以我添加了我能找到的所有状态。
最后,在选择一个项目时,调用mDrawerListView.setItemChecked(position, true);
除了自定义主题样式之外,一切正常。(最低API = 11,在API 17 AVD上测试)

2个回答

25

我也遇到了这个问题。问题是需要更改R.layout.fragment_navigation_drawer_list_item的背景,应将其更改为您的可绘制资源。只需在布局中添加android:background="@drawable/activated_background"即可。


谢谢!我简直不敢相信在其他答案中没有遇到过这个。我之前使用的是android:background=?android:activatedBackgroundIndicator - Flash

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),
        }))`

第二步。创建并自定义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" />

第三步。在您的 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>

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