Java-Android:所有活动上的导航抽屉

Java-Android:所有活动上的导航抽屉,第1张

概述我想在我的Android项目的所有活动上添加导航抽屉.这是MainActivity的代码:publicclassMainActivityextendsActivity{privateString[]drawerListViewItems;privateDrawerLayoutdrawerLayout;privateListViewdrawerListView;priva

我想在我的Android项目的所有活动上添加导航抽屉.这是MainActivity的代码:

public class MainActivity extends Activity {        private String[] drawerListVIEwItems;        private DrawerLayout drawerLayout;        private ListVIEw drawerListVIEw;        private ActionbarDrawerToggle actionbarDrawerToggle;        @OverrIDe        protected voID onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            setContentVIEw(R.layout.activity_main);            // get List items from strings.xml            drawerListVIEwItems = getResources().getStringArray(R.array.items);            // get ListVIEw defined in activity_main.xml            drawerListVIEw = (ListVIEw) findVIEwByID(R.ID.left_drawer);            // Set the adapter for the List vIEw            drawerListVIEw.setAdapter(new ArrayAdapter<String>(this,                    R.layout.drawer_ListvIEw_item, drawerListVIEwItems));            // App Icon             drawerLayout = (DrawerLayout) findVIEwByID(R.ID.drawer_layout);            //drawerLayout = (DrawerLayout) findVIEwByID(R.drawable.ic_drawer_2);            actionbarDrawerToggle = new ActionbarDrawerToggle(                    this,                  /* host Activity */                    drawerLayout,         /* DrawerLayout object */                    R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */                    R.string.drawer_open,  /* "open drawer" description */                    R.string.drawer_close  /* "close drawer" description */                    );            // Set actionbarDrawerToggle as the DrawerListener            drawerLayout.setDrawerListener(actionbarDrawerToggle);            getActionbar().setdisplayHomeAsUpEnabled(true);             // just styling option add shadow the right edge of the drawer        drawerLayout.setDrawerShadow(R.drawable.ic_drawer, GravityCompat.START);        drawerListVIEw.setonItemClickListener(new DrawerItemClickListener());    }    @OverrIDe        protected voID onPostCreate(Bundle savedInstanceState) {            super.onPostCreate(savedInstanceState);            // Sync the toggle state after onRestoreInstanceState has occurred.            actionbarDrawerToggle.syncState();        }        @OverrIDe        public voID onConfigurationChanged(Configuration newConfig) {            super.onConfigurationChanged(newConfig);            actionbarDrawerToggle.onConfigurationChanged(newConfig);        }        @OverrIDe        public boolean onoptionsItemSelected(MenuItem item) {             // call ActionbarDrawerToggle.onoptionsItemSelected(), if it returns true            // then it has handled the app icon touch event            if (actionbarDrawerToggle.onoptionsItemSelected(item)) {                return true;            }            return super.onoptionsItemSelected(item);        }        private class DrawerItemClickListener implements ListVIEw.OnItemClickListener {            @OverrIDe            public voID onItemClick(AdapterVIEw parent, VIEw vIEw, int position, long ID) {                    displayVIEw(position);                drawerLayout.closeDrawer(drawerListVIEw);            }            private voID displayVIEw(int position)             {                switch (position)                 {                case 0:                    secondactivity();                    break;                case 1:                    Toast.makeText(MainActivity.this, "2", Toast.LENGTH_LONG).show();                    break;                case 2:                    Toast.makeText(MainActivity.this, "3", Toast.LENGTH_LONG).show();                default:                    break;                }            }        }        public voID secondactivity (){            Intent cambioActivity;            cambioActivity = new Intent (this, SecondActivity.class);            startActivity(cambioActivity);        }}

在此代码中,我创建了导航抽屉,我希望所有活动都具有导航抽屉,所以我的第二个活动的代码是这样的:

public class SecondActivity extends MainActivity {    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_secondactivity);    }

导航抽屉位于第一个活动上,但是没有其他活动,为什么?有人能帮我吗?

解决方法:

简单的方法是您应该创建片段.如果您准备为小事做准备,那么这就是给您的.它将使您在所有活动中都具有相同的导航抽屉.

创建抽屉_n_activity.xml

<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent" ><FrameLayout    androID:ID="@+ID/drawer_frame"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent" /><YourDrawer    androID:ID="@+ID/drawer_drawer"    androID:layout_wIDth="match_parent"    androID:layout_height="fill_parent" ></YourDrawer></relativeLayout>

您的DrawerActivity.class

public class DrawerActivity extends Activity {    public relativeLayout fullLayout;    public FrameLayout frameLayout;    @OverrIDe    public voID setContentVIEw(int layoutResID) {        fullLayout = (relativeLayout) getLayoutInflater().inflate(R.layout.drawer_n_activity, null);        frameLayout = (FrameLayout) fullLayout.findVIEwByID(R.ID.drawer_frame);        getLayoutInflater().inflate(layoutResID, frameLayout, true);        super.setContentVIEw(fullLayout);        //Your drawer content...    }}

现在,要在所有活动中包含相同的导航抽屉,并且还要注意一件事,所有活动都必须扩展DrawerActivity

public class MainActivity extends DrawerActivity {    @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main); //layout for 1st activity   }}public class SecondActivity extends DrawerActivity {    @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.second_activity); //layout for 2nd activity   }}
总结

以上是内存溢出为你收集整理的Java-Android:所有活动上的导航抽屉全部内容,希望文章能够帮你解决Java-Android:所有活动上的导航抽屉所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存