【发布时间】:2014-04-16 14:28:10
【问题描述】:
这个问题在 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);
这里的上下文来自 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>
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 上测试)
【问题讨论】:
标签: android android-fragments android-theme