gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:32:25 24 4
gpt4 key购买 nike

这个问题在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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com