Android 带有“activatedBackgroundIndicator”导航抽屉的所选项目的自定义背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23112376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom background color for selected item with "activatedBackgroundIndicator" Navigation Drawer
提问by Flash
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.
这个问题在SO上被问了很多,我已经参考了所有的答案。我的导航抽屉上的选定项目仍然使用默认的 Holo 蓝色背景。我是 Java 新手,我对.setAdapter()
.
我的项目是单个 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. 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.
这里的上下文来自 Android Studio 中“预煮”的导航抽屉。我认为这将是所选项目的导航抽屉项目背景颜色的答案。所以我将我的上下文更改为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:
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>
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)
我不知道应该使用上述哪个状态,所以我添加了所有我能找到的状态。
最后,当一个项目被选中时mDrawerListView.setItemChecked(position, true);
被调用。
一切正常,除了自定义主题样式。(最小 API = 11,在 API 17 AVD 上测试)
回答by trevor-e
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.
我遇到了这个确切的问题。问题是背景R.layout.fragment_navigation_drawer_list_item
是需要更改为您的可绘制对象的内容。只需添加android:background="@drawable/activated_background"
到布局。
回答by u2tall
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,该 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>