问题描述
这个问题在SO上已经问了很多,我已经参考了所有答案.导航抽屉上的选定项目仍保留默认的Holo蓝色背景.我是Java新手,对.setAdapter()
的上下文"部分感到困惑.
我的项目是一个单个活动,使用导航抽屉交换了多个片段.
This question has been asked a lot on SO, and I have referenced all the answers. I am still left with the default Holo blue background for selected items on my navigation drawer. I am new to Java and I am confused about the "context" part of .setAdapter()
.
My project is a single Activity with multiple fragments swapped using the nav drawer.
这是我的适配器:
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中的预煮"导航抽屉.我认为这将是答案选定项目的导航抽屉项目背景颜色.因此,我将上下文更改为getActivity().getBaseContext(),
,但这并没有任何改变.
The context here comes from the "pre-cooked" navigation drawer in 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.
我的主题(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"目录中:
activated_background
in 'drawables' dir:
<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>
我不知道应该使用上述哪个状态,所以我添加了所有可以找到的状态.
最后,当一个项目被选中被调用.结果一切正常,除了自定义主题样式. (最小API = 11,在API 17 AVD上进行测试)
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);
is called.
Everything works, except for the custom theme styling. (min API = 11, testing on API 17 AVD)
推荐答案
我遇到了这个确切的问题.问题在于R.layout.fragment_navigation_drawer_list_item
的背景需要更改为可绘制对象.只需在布局中添加android:background="@drawable/activated_background"
.
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. Simply add android:background="@drawable/activated_background"
to the layout.
这篇关于带有"activatedBackgroundIndicator"的所选项目的自定义背景色;导航抽屉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!