Android导航抽屉,更改文本/悬停颜色 [英] Android navigation drawer, change text/hover color

查看:88
本文介绍了Android导航抽屉,更改文本/悬停颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于向Android Studio提供的导航抽屉模板,我有两个问题.

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

)

我想更改菜单的文本颜色("notre histoire"等)和所选项目的悬停(此处为绿色,我想将其设置为其他颜色).

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.

我的约束是我无法触摸xml文件.我必须以编程方式进行.

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

那么,现在如何用一些代码行更改文本颜色和悬停颜色而不创建/更新一些xml文件?

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

谢谢=)

推荐答案

您可以编写自己的列表适配器来代替使用android的默认ArrayAdapter:

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;
}

}

现在,在if语句(isItemChecked)中,您可以更改文本视图的背景颜色.

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

这篇关于Android导航抽屉,更改文本/悬停颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆