简体   繁体   English

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

[英]Custom background color for selected item with “activatedBackgroundIndicator” Navigation Drawer

This question has been asked a lot on SO, and I have referenced all the answers. 这个问题在SO上已经被问了很多,我已经引用了所有的答案。 I am still left with the default Holo blue background for selected items on my navigation drawer. 我的导航抽屉上的所选项目仍然保留默认的Holo蓝色背景。 I am new to Java and I am confused about the "context" part of .setAdapter() . 我是Java的新手,我对.setAdapter()的“context”部分感到困惑。
My project is a single Activity with multiple fragments swapped using the nav drawer. 我的项目是一个Activity,使用导航抽屉交换了多个片段。

Here is my adapter: 这是我的适配器:

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

The context here comes from the "pre-cooked" navigation drawer in Android Studio. 这里的上下文来自Android Studio中的“预先烹饪”导航抽屉。 I thought this would be the answer Navigation Drawer item background colour for selected item . 我认为这将是所选项目的导航抽屉项目背景颜色的答案。 So I changed my context to getActivity().getBaseContext(), , but that didn't change anything. 所以我将我的上下文改为getActivity().getBaseContext(), ,但这并没有改变任何东西。

My theme ( styles.xml ): 我的主题( 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>

activated_background in 'drawables' dir: '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>

I don't know which of those above states is supposed to be used, so I added all of them I could find. 我不知道应该使用上述哪些状态,所以我添加了我能找到的所有状态。
Finally, when an item is selected mDrawerListView.setItemChecked(position, true); 最后,当一个项目被选中时mDrawerListView.setItemChecked(position, true); is called. 叫做。
Everything works, except for the custom theme styling. 一切都有效,除了自定义主题样式。 (min API = 11, testing on API 17 AVD) (最小API = 11,在API 17 AVD上测试)

I have run into this exact problem. 我遇到了这个问题。 The issue is that the background of R.layout.fragment_navigation_drawer_list_item is what needs to be changed to your drawable. 问题是R.layout.fragment_navigation_drawer_list_item的背景是​​需要更改为drawable的。 Simply add android:background="@drawable/activated_background" to the layout. 只需将android:background="@drawable/activated_background"到布局中即可。

I wasn't able to find any full clear answer to this question, so here it is: 我无法找到这个问题的任何完整明确的答案,所以这里是:

Step 1. Specify the list item layout your adapter will use, in this example we're specifying: R.layout.fragment_navigation_drawer_list_item 步骤1.指定适配器将使用的列表项布局,在此示例中我们指定: R.layout.fragment_navigation_drawer_list_item

Like so: 像这样:

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

Step 2. Create and customize fragment_navigation_drawer_list_item.xml to specify a drawable that will have a selector like so: android:background="@drawable/activated_background" Full example: 步骤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" />

Step 3. Create and customize the selector in your activated_background.xml file just like in the question, it will look like this: 步骤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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有选定背景颜色的导航抽屉项目分隔符 - Navigation Drawer item divider with selected background color 更改导航抽屉中所选项目的背景颜色 - Change background color of selected item in navigation drawer 如何在activatedBackgroundIndicator中的选定/突出显示的列表项上设置自定义颜色 - How to set custom color on selected/highlighted list-item in activatedBackgroundIndicator 如何为导航抽屉上的不同所选项目设置不同的背景颜色? - How to set different background color for different selected item on Navigation Drawer? 无法在导航抽屉中更改所选项目的背景颜色 - Can't change selected item's background color in Navigation Drawer Android如何为导航抽屉中的选定项目设置背景颜色? - Android how to set the background color for selected item in navigation drawer? 如何更改导航抽屉中所选项目的背景颜色 - How to change the Background color of selected item in the Navigation Drawer 如何在android导航抽屉中更改所选项目的背景颜色? - How to change the selected item's background color in android navigation drawer? 所选项目的导航抽屉项目背景颜色 - Navigation Drawer item background colour for selected item android-导航抽屉上的选定项目颜色 - android - Selected Item Color on Navigation Drawer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM