2014-09-10 198 views
0

我想改變導航抽屜片段上的高光顏色。我爲導航抽屜使用了預建的項目模板。我已經搜索了許多不同的解決方案,但到目前爲止,他們都沒有工作。目前,這是我所:Android導航抽屜選擇器顏色

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

你應該改變項目的位置,就像這樣:

<?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> 
+0

感謝,這也讓這個當我點擊它暫時改變顏色的菜單項。但是,這種顏色不會留下。 – Kyle 2014-09-10 17:22:48

+0

你需要添加 android:listSelector =「@ android:color/black」到你的列表視圖xml – MHP 2014-09-10 17:25:04

+0

好吧我嘗試添加到我的fragment_navigation_drawer.xml,但它沒有改變任何東西。 – Kyle 2014-09-10 17:28:01