android - 带有 "activatedBackgroundIndicator"抽屉导航的所选项目的自定义背景颜色

标签 android android-fragments android-theme

这个问题在SO上被问了很多,我已经引用了所有的答案。我的抽屉导航中的选定项目仍然保留默认的全息蓝色背景。我是 Java 的新手,我对 .setAdapter() 的“上下文”部分感到困惑。
我的项目是使用抽屉导航交换多个 fragment 的单个 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 中的“pre-cooked”抽屉导航。我认为这就是答案 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>

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>

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

最佳答案

我遇到了这个确切的问题。问题是 R.layout.fragment_navigation_drawer_list_item 的背景需要更改为您的可绘制对象。只需将 android:background="@drawable/activated_background" 添加到布局中即可。

关于android - 带有 "activatedBackgroundIndicator"抽屉导航的所选项目的自定义背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23112376/

相关文章:

java - Android:Realm了解LinkingObjects

java - 在 java 或 UE4 c++ 中获取三星 Galaxy S7 的真实屏幕分辨率

Android 如何在单击 RecyclerView 项目时从 fragment 发送 Intent

android - 操作栏外的下拉微调器? ( Ice Cream Sandwich 风格,带 ActionBarSherlock)

android - AlertDialog 样式 - 如何更改标题、消息等的样式(颜色)

android - 当用户在 android 中点击它时,如何从自动完成 TextView 中失去焦点并获得焦点?

Android:如果应用程序被系统杀死,Alarm Manager 是否会被杀死

java - android.support.v4.app.fragment 和 androidx.fragment.app.FragmentActivity 有什么区别

java - 类播异常 : Fragment_main cannot be cast to fragment activity android