Android导航抽屉,更改文字/悬停颜色

时间:2015-11-16 12:49:05

标签: java android android-navigation-drawer

我有两个关于导航抽屉模板的问题,它给了android studio。

ScreenShot of the app

我想要更改菜单的文字颜色(“notre histoire”等)和所选项目的悬停(这里是绿色,我想用其他颜色制作)。

正如你所看到的,我设法改变了动作栏的背景颜色(这里是粉红色)并改变了菜单的背景(这里是蓝色)。

但在我的情况下,我没有找到如何更改文本颜色和所选项目的悬停。

我的约束是我无法触摸xml文件。我必须以编程方式完成它。

以下是我将菜单字符串提供给应用的方式:

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文件呢?

谢谢=)

1 个答案:

答案 0 :(得分:2)

您可以编写自己的列表适配器,而不是使用Android的默认ArrayAdapter:

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)中,您现在可以更改文本视图的背景颜色。

相关问题