Changing Navigation Drawer Selected Item Color from default blue

前端 未结 2 721
我在风中等你
我在风中等你 2021-01-11 12:17

Hi and thanks for reading.

I have a problem with my android apps navigation drawer where I cannot change the color from blue - I have went over all the other questi

相关标签:
2条回答
  • 2021-01-11 12:52

    ListDrawer onItemClickListener:

        final CustomListAdapter myadapter;
         mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectItem(position);
                myadapter.setSelectedItem(position);
            }
    

    In custom adapter:

    public class CustomListAdapter extends ArrayAdapter<String> {
        private final Activity context;
        private final String[] itemname;
    
        int mSelectedItem;
    
        public CustomListAdapter(Activity context, String[] itemname ) {
            super(context, R.layout.drawer_list_item, itemname);
            // TODO Auto-generated constructor stub
    
            this.context = context;
            this.itemname = itemname;
    
        }
    
        public void setSelectedItem(int selectedItem) {
            this.mSelectedItem = selectedItem;
        }
    
        public View getView(final int position, View view, ViewGroup parent) {
            LayoutInflater inflater = context.getLayoutInflater();
            View rowView = inflater.inflate(R.layout.drawer_list_item, null, true);
            TextView txtTitle = (TextView) rowView.findViewById(R.id.textitem);
            String iname = itemname[position];
            txtTitle.setText(iname);
            txtTitle.setTypeface(tf);
            if (position == mSelectedItem) {
                txtTitle.setTextColor(getContext().getResources().getColor(R.color.white));
            } else {
                txtTitle.setTextColor(getContext().getResources().getColor(R.color.normal));
            }
            return rowView;
        }
    }
    

    In colors.xml

        <resources>
            <color name="white">#ffffff</color>
            <color name="normal">#ef3272</color>
       </resources>
    
    0 讨论(0)
  • 2021-01-11 12:53

    First, I would try removing the android:listSelector attribute, as I don't believe it is necessary.

    Next, I would double check you have all these steps:

    • In your application's theme, try adding

    themes.xml

    <style name="Theme.mytheme" parent="android:Theme.Holo">
        <item name="android:activatedBackgroundIndicator">@drawable/activated_background</item>
    </style>
    
    • The drawable should refer to a file containing a selector like (like you have)

    activated_background.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@color/my_color" android:state_activated="true" />
        <item android:drawable="@android:color/transparent" />
    </selector>
    

    colors.xml

    <resources>
        <item name="my_color" type="color">#ff0000</item>
    </resources>
    
    • Finally, make sure you are applying the theme in your manifest's application tag using

    AndroidManifest.xml

    android:theme="@style/Theme.mytheme"
    
    0 讨论(0)
提交回复
热议问题