我可以知道如何更改导航抽屉中选定项目的文本颜色。自定义适配器导航抽屉选定项目textColor
的机器人:文字颜色为列表视图不工作后安装appcompat_v7 22.1.1 ,所以我想到了用选择绘制我不能改变。
我设法通过使用自定义适配器来更改抽屉项目的默认颜色。
但是,这只是设法初始化时将第一项设置为绿色。
我可以知道如何将所选项目(onclick)的颜色更改为选择项目时所需的颜色。
适配器:
public class NavigationDrawerAdapter extends ArrayAdapter<String> {
private int mSelectedItem;
private final Context context;
private String[] sections;
public NavigationDrawerAdapter(Context context, int resource, String[] objects) {
super(context, resource, objects);
this.context = context;
sections = objects;
}
public int getSelectedItem() {
return mSelectedItem;
}
public void setSelectedItem(int selectedItem) {
mSelectedItem = selectedItem;
}
private String getMenuItemTitle(int position) {
if (position >= 0 && position <= sections.length)
return sections[position];
return "Invalid section requested";
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Get item TextView
TextView view = (TextView) super.getView(position, convertView, parent);
view.setText(getMenuItemTitle(position));
if (position == mSelectedItem) {
view.setTextColor(getContext().getResources().getColor(R.color.green));
} else {
view.setTextColor(getContext().getResources().getColor(R.color.white)); }
return view;
}
}
抽屉式导航:
mDrawerListView.setAdapter(new NavigationDrawerAdapter(
getActionBar().getThemedContext(),
android.R.layout.simple_list_item_activated_1,
new String[]{
getString(R.string.title_home),
getString(R.string.title_stamp),
getString(R.string.title_scanner),
getString(R.string.title_setting),}) {
});
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView;
..
...
private void selectItem(int position) {
mCurrentSelectedPosition = position;
if (mDrawerListView != null) {
mDrawerListView.setItemChecked(position, true);
}
if (mDrawerLayout != null) {
mDrawerLayout.closeDrawer(mFragmentContainerView);
}
if (mCallbacks != null) {
mCallbacks.onNavigationDrawerItemSelected(position);
}
}