Android navigation drawer, change text/hover color

不打扰是莪最后的温柔 提交于 2019-12-08 07:44:59

问题


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 =)


回答1:


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.



来源:https://stackoverflow.com/questions/33735820/android-navigation-drawer-change-text-hover-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!