如何在导航栏

时间:2016-09-27 11:11:48

标签: android navigation-drawer

我正在开发一个应用程序,其中我正在使用导航抽屉,因为当我试图为项目添加图标时,它正在给我这样的图像路径:“ res / drawale / bg.jpg“。我不知道怎么做。

以下是我的主要活动代码:

 private void init_navigator() {
    // Navigation Drawer
    mTitle = mDrawerTitle = getTitle();

    mDrawerLayout = (DrawerLayout) findViewById(R.id.main_activity_DrawerLayout);
    mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.primaryDark));
    mScrimInsetsFrameLayout = (ScrimInsetsFrameLayout) findViewById(R.id.main_activity_navigation_drawer_rootLayout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    mDrawerItmes = getResources().getStringArray(R.array.drawer_titles);
  mDrawerItmes = getResources().getStringArray(R.array.nav_drawer_icons);

这是我的string.xml代码

   <string-array name="drawer_titles">
    <item>About Us</item>
    <item>FeedBack</item>
    <item>Setting</item>
    <item>Share App</item>
    <item>Rate Us</item>
    <item>Logout</item>
</string-array>

<array name="nav_drawer_icons">
    <item>@drawable/bg</item>
    <item>@drawable/bg</item>
    <item>@drawable/bg</item>
    <item>@drawable/bg</item>
    <item>@drawable/b</item>
    <item>@drawable/bg</item>

</array>

2 个答案:

答案 0 :(得分:3)

在xml的导航视图中添加:

app:menu="@menu/drawer_menu_icons"

然后在菜单文件夹中创建一个名为drawer_menu_icons的新xml,并执行以下操作:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group
        android:id="@+id/homegroup"
        android:checkableBehavior="single">
        <item
            android:id="@+id/nav_home"
            android:icon="@drawable/homeicon"
            android:title="Main screen" />
        <item
            android:id="@+id/nav_branches"
            android:icon="@drawable/backspace"
            android:title="Branch selection" />

    </group>
</menu>

并添加您想要的任何图标:)

答案 1 :(得分:0)

试着按照这种方式,这正是你要找的东西

http://www.journaldev.com/9958/android-navigation-drawer-example-tutorial

http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

public class DrawerItemCustomAdapter extends ArrayAdapter<DataModel> {

    Context mContext;
    int layoutResourceId;
    DataModel data[] = null;

    public DrawerItemCustomAdapter(Context mContext, int layoutResourceId, DataModel[] data) {

        super(mContext, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.mContext = mContext;
        this.data = data;
    }

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

        View listItem = convertView;

        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
        listItem = inflater.inflate(layoutResourceId, parent, false);

        ImageView imageViewIcon = (ImageView) listItem.findViewById(R.id.imageViewIcon);
        TextView textViewName = (TextView) listItem.findViewById(R.id.textViewName);

        DataModel folder = data[position];


        imageViewIcon.setImageResource(folder.icon);
        textViewName.setText(folder.name);

        return listItem;
    }
}