简体   繁体   中英

Android navigation drawer, change text/hover color

I have two questions about the navigation drawer template that give android studio.

应用程序截图 )

I want change the text color of the menus ("notre histoire" etc.) and the hover of selected item (here it's green, i want make that in a other color).

As you can see, i managed to change the background color of the the action bar (here in pink) and change the background of the menu (here in blue).

But in my situation i didn't find how i can change the text color and the hover of selected items.

My constraint is that i don't can touch the xml files. I must do it programmatically.

Here is how i give my menus strings to the app :

String [] strTabMenu = new String[2];
strTabMenu[0] = "test1";
strTabMenu[1] = "test2";

mDrawerListView.setAdapter(new ArrayAdapter<String>(
                getActionBar().getThemedContext(),
                android.R.layout.simple_list_item_activated_1,
                android.R.id.text1,
                strTabMenu));

So, how can i now, with some code line, change the text color and the hover color without creating/updating some xml files ?

Thanks =)

Instead of using the default ArrayAdapter from android you could write your own list adapter:

public class DrawerListAdapter extends BaseAdapter{

private Context context;
private String[] mTitle;
private int[] mIcon;
private LayoutInflater inflater;

public DrawerListAdapter(Context pContext, String[] pTitle, int[] pIcon) {
    super();
    context = pContext;
    mTitle = pTitle;
    mIcon = pIcon;
}

public View getView(int position, View convertView, ViewGroup parent) {
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rootView = inflater.inflate(R.layout.navigation_drawer_list_item, parent, false);

    TextView txtTitle = (TextView) rootView.findViewById(R.id.drawer_text);
    ImageView imgIcon = (ImageView) rootView.findViewById(R.id.drawer_icon);

    if(((ListView)parent).isItemChecked(position)) {
            txtTitle.setTextColor(parent.getResources().getColor(R.color.DarkerRed));
    }
    txtTitle.setText(mTitle[position]);
    imgIcon.setImageResource(mIcon[position]);

    return rootView;
}

@Override
public int getCount() {
    return mTitle.length;
}

@Override
public Object getItem(int position) {
    return mTitle[position];
}

@Override
public long getItemId(int position) {
    return position;
}

}

Inside the if-statement ( isItemChecked) you could now change the background color of your text view.

To change the hover item background I had to change the primary color, the item text color however I was able to change it with

// change hover text color
ColorStateList csl = new ColorStateList(
 int[][] {

new int[] {-android.R.attr.state_checked}, // unchecked
new int[] { android.R.attr.state_checked}  // checked 
},

new int[] {
getResources().getColor(R.color.color1, 
getTheme()), getResources().getColor(R.color.color2, getTheme())
});
        
navigationView.setItemTextColor(csl);
navigationView.setItemIconTintList(csl);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM