如何将图标设置为自定义导航抽屉菜单

时间:2019-03-20 13:53:22

标签: android menu icons navigation-drawer

如何在自定义导航抽屉中的标题旁边显示图标?

我已经创建了自定义导航抽屉,其中我正在使用menuAdaptermenuList,如下所示-

public class MainActivity extends AppCompatActivity implements CustomNavigationView.NavigationItemSelectedListner {
ArrayList<String> menuList;
ArrayAdapter<String> menuAdapter;
CustomNavigationView navView;
DrawerLayout drawerLayout;
android.support.v4.app.FragmentTransaction ft;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);
    navView = findViewById(R.id.navView);
    drawerLayout = findViewById(R.id.activity_main);
    menuAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, menuList);
    navView.setAdapter(menuAdapter);
    navView.setHeaderView(getHeader(), 20);
    navView.setOnNavigationItemSelectedListner(this);
    navView.setScrollState(CustomNavigationView.MENU_ITEM_SCROLLABLE);
    navView.setSelectionBackGround(getResources().getColor(R.color.colorAccent));
    drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            drawerLayout.getChildAt(0).setTranslationX(slideOffset * drawerView.getWidth());
            drawerLayout.bringChildToFront(drawerView);
            drawerLayout.requestLayout();
        }
    });
}

private void prepareListItems() {
    menuList = new ArrayList<>();
    menuList.add("One");
    menuList.add("Two");
    menuList.add("Three");
    menuList.add("Four");
}

在这里,我正在借助menuList.add("One");方法添加标题,如下图所示,但是我想添加带有标题的图标/项目符号/徽标。

>

检查此输出:

enter image description here

我该如何添加,请帮助我。预先感谢!

1 个答案:

答案 0 :(得分:0)

您可以像这样在menu.xml文件中设置图标:-

<item android:id="@+id/logout" android:title="Log Out" android:icon="@drawable/ic_logout"/>