android – 在导航抽屉中添加图标

android – 在导航抽屉中添加图标,第1张

概述我有一个导航抽屉,我想添加图标.但我不知道如何实现它.有什么办法可以自定义导航抽屉里面的列表吗?并使用菜单而不是数组导航抽屉内的项目.提前致谢 :) 继承我的代码: public class MainActivity extends ActionBarActivity {private ListView mDrawerList;private DrawerLayout mDrawerLayo 我有一个导航抽屉,我想添加图标.但我不知道如何实现它.有什么办法可以自定义导航抽屉里面的列表吗?并使用菜单而不是数组导航抽屉内的项目.提前致谢 :)

继承我的代码:

public class MainActivity extends ActionBaractivity {private ListVIEw mDrawerList;private DrawerLayout mDrawerLayout;private ArrayAdapter<String> mAdapter;private ActionbarDrawerToggle mDrawerToggle;private String mActivityTitle;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    mDrawerList = (ListVIEw)findVIEwByID(R.ID.navList);    mDrawerLayout = (DrawerLayout)findVIEwByID(R.ID.drawer_layout);    mActivityTitle = getTitle().toString();    addDrawerItems();    setupDrawer();    FragmentManager fragmentManager = getSupportFragmentManager();    fragmentManager.beginTransaction().replace(R.ID.flContent,new One()).commit();    getSupportActionbar().setdisplayHomeAsUpEnabled(true);    getSupportActionbar().setHomebuttonEnabled(true);     }    private voID addDrawerItems() {    String[] osArray = { "AndroID","iOS","windows","OS X","linux" };    mAdapter = new ArrayAdapter<String>(this,androID.R.layout.simple_List_item_1,osArray);    mDrawerList.setAdapter(mAdapter);    mDrawerList.setonItemClickListener(new AdapterVIEw.OnItemClickListener() {        Fragment fragment = null;        Class fragmentClass;        @OverrIDe        public voID onItemClick(AdapterVIEw<?> parent,VIEw vIEw,int position,long ID) {            switch (position) {                case 1:                    fragmentClass = One.class;                    break;                case 2:                    fragmentClass = Two.class;                    break;                default:                    break;            }            try {                fragment = (Fragment) fragmentClass.newInstance();            } catch (Exception e) {                e.printstacktrace();            }            // Insert the fragment by replacing any existing fragment            FragmentManager fragmentManager=getSupportFragmentManager();            fragmentManager.beginTransaction().replace(R.ID.flContent,fragment).commit();            // Highlight the selected item,update the Title,and close the drawer           mDrawerLayout.closeDrawers();        }    });         }      private voID setupDrawer() {      mDrawerToggle = new ActionbarDrawerToggle(this,mDrawerLayout,R.string.drawer_open,R.string.drawer_close) {        /** Called when a drawer has settled in a completely open state. */        public voID onDrawerOpened(VIEw drawerVIEw) {            super.onDrawerOpened(drawerVIEw);            getSupportActionbar().setTitle("Navigation!");            invalIDateOptionsMenu(); // creates call to onPrepareOptionsMenu()        }        /** Called when a drawer has settled in a completely closed state. */        public voID onDrawerClosed(VIEw vIEw) {            super.onDrawerClosed(vIEw);            getSupportActionbar().setTitle(mActivityTitle);            invalIDateOptionsMenu();      // creates call to onPrepareOptionsMenu()        }    };    mDrawerToggle.setDrawerIndicatorEnabled(true);    mDrawerLayout.setDrawerListener(mDrawerToggle);}@OverrIDeprotected voID onPostCreate(Bundle savedInstanceState) {    super.onPostCreate(savedInstanceState);    // Sync the toggle state after onRestoreInstanceState has occurred.    mDrawerToggle.syncState();}@OverrIDepublic voID onConfigurationChanged(Configuration newConfig) {    super.onConfigurationChanged(newConfig);    mDrawerToggle.onConfigurationChanged(newConfig);  }    @OverrIDe   public boolean onCreateOptionsMenu(Menu menu) {    // Inflate the menu; this adds items to the action bar if it is present.    getMenuInflater().inflate(R.menu.menu_main,menu);    return true;}@OverrIDepublic boolean onoptionsItemSelected(MenuItem item) {    // Handle action bar item clicks here. The action bar will    // automatically handle clicks on the Home/Up button,so long    // as you specify a parent activity in AndroIDManifest.xml.    int ID = item.getItemID();    //noinspection SimplifiableIfStatement    if (ID == R.ID.action_settings) {        return true;    }    // Activate the navigation drawer toggle    if (mDrawerToggle.onoptionsItemSelected(item)) {        return true;    }    return super.onoptionsItemSelected(item);} }
解决方法 在你的导航抽屉xml文件里面添加菜单文件app:menu =“@ menu / activity_main_drawer”

<androID.support.design.Widget.NavigationVIEw            androID:ID="@+ID/nav_vIEw"            androID:layout_wIDth="wrap_content"            androID:layout_height="match_parent"            androID:fitsSystemwindows="true"            app:menu="@menu/activity_main_drawer">        </androID.support.design.Widget.NavigationVIEw>

在activity_main_drawer菜单文件中,用他们的名字定义你的图标

<?xml version="1.0" enCoding="utf-8"?><menu xmlns:androID="http://schemas.androID.com/apk/res/androID">    <group androID:checkableBehavior="single">        <item androID:ID="@+ID/nav_camara" androID:icon="@androID:drawable/ic_menu_camera"            androID:title="Camera" />        <item androID:ID="@+ID/nav_gallery" androID:icon="@androID:drawable/ic_menu_gallery"            androID:title="gallery" />        <item androID:ID="@+ID/nav_manage" androID:icon="@androID:drawable/ic_menu_manage"            androID:title="Manage" />    </group>

在MainActivity.java中调用每个项目的 *** 作单击抽屉菜单

@OverrIDe    public boolean onNavigationItemSelected(MenuItem menuItem) {        // Handle navigation vIEw item clicks here.        int ID = menuItem.getItemID();        if (ID == R.ID.nav_camara) {                      // Call your Action        } else if (ID == R.ID.nav_gallery) {                      // Call your Action        } else if (ID == R.ID.nav_manage) {                       // Call your Action           }                }
总结

以上是内存溢出为你收集整理的android – 在导航抽屉中添加图标全部内容,希望文章能够帮你解决android – 在导航抽屉中添加图标所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1123661.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存