Android导航抽屉选择器颜色

时间:2014-09-10 17:16:14

标签: android android-fragments navigation-drawer

我正在尝试更改导航抽屉片段上的突出显示颜色。我已经为导航抽屉使用了预先构建的项目模板。我搜索过这个网站有很多不同的解决方案,但到目前为止还没有一个有效。目前,这就是我所拥有的:

list_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@color/ListBackground" android:state_activated="false"></item>
    <item android:drawable="@color/OrangeBackground" android:state_pressed="true"></item>
    <item android:drawable="@color/OrangeBackground" android:state_activated="true"></item>
</selector>

NavigationDrawerFragment.java 我只发布了相关的mDrawerList构造函数/ decleration。

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        mDrawerListView = (ListView) inflater.inflate(
                R.layout.fragment_navigation_drawer, container, false);
        mDrawerListView.setSelector(R.drawable.list_selector);
        mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectItem(position);
                view.setSelected(true);
            }
        });
        mDrawerListView.setAdapter(new ArrayAdapter<String>(
                getActionBar().getThemedContext(),
                android.R.layout.simple_list_item_activated_1,
                android.R.id.text1,
                new String[]{
                        getString(R.string.title_section1),
                        getString(R.string.title_section2),
                        getString(R.string.title_section3),
                        getString(R.string.title_section4),
                        getString(R.string.title_section5),
                        getString(R.string.title_section6)
                }));
        mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
        return mDrawerListView;
    }

1 个答案:

答案 0 :(得分:1)

您应该更改项目位置,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@color/OrangeBackground" android:state_pressed="true"></item>
    <item android:drawable="@color/OrangeBackground" android:state_activated="true"></item>
    <item android:drawable="@color/ListBackground"></item>
</selector>