更改导航抽屉列表文本颜色并添加图标

时间:2015-05-30 23:44:39

标签: android

关于堆栈溢出,我没有找到任何关于这个问题的好答案。我正在使用Android Studio默认导航抽屉布局。我尝试从fragment_navigation.xml更改textView的颜色,但这没有帮助。我不知道在NavigationDrawerFragment.java中要做什么来改变颜色。也许需要在下面的代码中进行一些编辑。

  mDrawerListView.setAdapter(new ArrayAdapter<String>(
            getActionBar().getThemedContext(),
            android.R.layout.simple_list_item_activated_1,
            android.R.id.text1,
            new String[]{
                    getString(R.string.title_section1),
                    getString(R.string.title_section2),
                    getString(R.string.title_section3),
                    getString(R.string.title_section4),
                    getString(R.string.title_section5),
                    getString(R.string.title_section6)
            }));

    ViewGroup header = (ViewGroup) inflater.inflate(R.layout.headers, mDrawerListView,false);

    mDrawerListView.addHeaderView(header, null, false);
    mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
    return mDrawerListView;
}

此外,我无法找到任何有关添加图标以及导航抽屉列表中的文本的好教程。请帮助我。

2 个答案:

答案 0 :(得分:3)

您需要做的就是创建自定义适配器,而不是使用默认代码中的ArrayAdapter。

我刚刚得到了一个使用扩展ArrayAdapter的自定义适配器的简单示例。

首先,您需要自定义适配器中每行的布局。

nav_drawer_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/icon"
        android:paddingTop="20dp"
        android:layout_width="35dp"
        android:layout_height="55dp" />

    <TextView
        android:id="@+id/text"
        android:textStyle="bold"
        android:textColor="#ff69b4"
        android:paddingTop="25dp"
        android:layout_marginLeft="4dp"
        android:layout_width="wrap_content"
        android:layout_height="55dp" />

</LinearLayout>

然后,在NavigationDrawerFragment.java中,定义自定义适配器,并使用它而不是默认的ArrayAdapter:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    mDrawerListView = (ListView) inflater.inflate(
            R.layout.fragment_navigation_drawer, container, false);
    mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    });


    //Use Custom Adapter instead:
    mDrawerListView.setAdapter(new CustomNavAdapter(
            NavigationDrawerFragment.this.getActivity(),
            new String[]{
                    getString(R.string.title_section1),
                    getString(R.string.title_section2),
                    getString(R.string.title_section3),
            },
            new Integer[]{
                    R.drawable.ic_launcher,
                    R.drawable.ic_launcher,
                    R.drawable.ic_launcher,
            }));

    mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
    return mDrawerListView;
}


public class CustomNavAdapter extends ArrayAdapter<String> {
    private final Activity _context;
    private final String[] _text;
    private final Integer[] _imageId;

    public CustomNavAdapter(Activity context, String[] text, Integer[] imageId) {
        super(context, R.layout.nav_drawer_item, text);
        this._context = context;
        this._text = text;
        this._imageId = imageId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = _context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.nav_drawer_item, null, true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.text);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
        txtTitle.setText(_text[position]);
        imageView.setImageResource(_imageId[position]);

        return rowView;
    }

}

结果:

Custom Navigation Drawer

答案 1 :(得分:0)