如何更改navigationDrawer的TextView中图标和文本的颜色?

时间:2015-05-11 00:50:32

标签: android material-design

我正在尝试遵循材料设计指南,但我不知道如何更改导航抽屉的图标和文字的颜色。

(向下滚动到选择状态): http://www.google.com/design/spec/patterns/navigation-drawer.html#navigation-drawer-content

这来自我的MainActivity(在onCreate()中):

    protected void onCreate(Bundle savedInstanceState) {
    ...
    ListAdapter adapter = new MyAdapter(this, menu, imageID);
    ListView listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            mAdapter.setSelectedItem(position);
        }
    });
   ...
   }
来自MyAdapter()的

public class MyAdapter extends ArrayAdapter<String>{

private Integer[] imageID;
private int mSelectedItem;

public MyAdapter(Context context, String[] menu, Integer[] imageID) {
    super(context, R.layout.row_layout, menu);
    this.imageID = imageID;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(getContext());

    View view = inflater.inflate(R.layout.row_layout, parent, false);

    String menu = getItem(position);
    TextView textView = (TextView) view.findViewById(R.id.textView1);
    textView.setText(menu);

    if (position == getSelectedItem()) {
        textView.setTextColor(getContext().getResources().getColor(R.color.primaryColor));
    }
    else {
        textView.setTextColor(getContext().getResources().getColor(R.color.navDrawerTextColor));
    }

    ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
    imageView.setImageResource(imageID[position]);

    return view;
}

public int getSelectedItem() {
    return mSelectedItem;
}

public void setSelectedItem(int selectedItem) {
    mSelectedItem = selectedItem;
}

}

和我的TextView来自xml文件:

    <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_gravity="center_vertical"
    android:fontFamily="sans-serif-medium"
    android:paddingStart="72dp"
    android:paddingEnd="16dp"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@color/color_selector"
    android:textSize="14sp" />

感谢任何帮助,提前谢谢!

编辑:我使用了color_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/primaryColor" android:state_pressed="true" />
    <!-- pressed -->
    <item android:color="@color/primaryColor" android:state_selected="true" />
    <!-- selected -->
    <item android:color="#DE000000" />
    <!-- default -->
</selector>

但是当选择该项目时,它不会永久保留该颜色

1 个答案:

答案 0 :(得分:0)

您应该考虑使用NavigationViewAndroid Design Support Library的一部分,为您完成导航抽屉的所有项目选择和样式设置。