将导航抽屉活动模板添加到现有项目

时间:2016-07-27 14:46:13

标签: android xml android-studio

我目前有一个现有项目,我希望在其中实现导航抽屉活动。目前,如果我添加新的导航抽屉活动,它会生成以下布局:

Navigation Drawer Activity

然而,除了上面的布局,我希望我的导航抽屉看起来与默认模板完全相同(包括导航标题等):

enter image description here

我该如何实现这一目标?我是Android开发的新手。

编辑:我只需要更改我的应用主题以实现我想要的

2 个答案:

答案 0 :(得分:1)

创建一个新的抽屉活动:

文件 - > new->活动 - >导航抽屉活动

答案 1 :(得分:0)

private void setupNavigationDrawer() {
        //if you want to update the items at a later time it is recommended to keep it in a variable
        PrimaryDrawerItem item1 = new PrimaryDrawerItem().withIdentifier(0).withName(R.string.drawer_item_home).withIcon(getResources().getDrawable(R.drawable.ic_home_white_24dp));
        PrimaryDrawerItem item2 = new PrimaryDrawerItem().withIdentifier(1).withName(R.string.drawer_item_login).withIcon(getResources().getDrawable(R.drawable.ic_account_circle_white_24dp));
        PrimaryDrawerItem item3 = new PrimaryDrawerItem().withIdentifier(2).withName(R.string.drawer_item_movies).withIcon(getResources().getDrawable(R.drawable.ic_movie_white_24dp));
        PrimaryDrawerItem item4 = new PrimaryDrawerItem().withIdentifier(3).withName(R.string.drawer_item_trailers).withIcon(getResources().getDrawable(R.drawable.ic_videocam_white_24dp));
        PrimaryDrawerItem item5 = new PrimaryDrawerItem().withIdentifier(4).withName(R.string.drawer_item_theatres).withIcon(getResources().getDrawable(R.drawable.ic_theaters_white_24dp));
        PrimaryDrawerItem item6 = new PrimaryDrawerItem().withIdentifier(5).withName(R.string.drawer_item_location).withIcon(getResources().getDrawable(R.drawable.ic_location_on_white_24dp));
        SecondaryDrawerItem item7 = (SecondaryDrawerItem) new SecondaryDrawerItem().withIdentifier(6).withName(R.string.drawer_item_about_us).withIcon(FontAwesome.Icon.faw_info_circle);
        SecondaryDrawerItem item8 = (SecondaryDrawerItem) new SecondaryDrawerItem().withIdentifier(7).withName(R.string.drawer_item_contact_us).withIcon(FontAwesome.Icon.faw_whatsapp);
        SecondaryDrawerItem item9 = (SecondaryDrawerItem) new SecondaryDrawerItem().withIdentifier(8).withName(R.string.drawer_item_feedback).withIcon(FontAwesome.Icon.faw_commenting);
        SecondaryDrawerItem item10 = (SecondaryDrawerItem) new SecondaryDrawerItem().withIdentifier(9).withName(R.string.drawer_item_settings).withIcon(FontAwesome.Icon.faw_wrench);

        // Create the AccountHeader
        AccountHeader headerResult = new AccountHeaderBuilder()
                .withActivity(this)
                .withHeaderBackground(R.color.colorMaterialDark)
                .addProfiles(
                        new ProfileDrawerItem().withName(getResources().getString(R.string.app_name)).withEmail(getResources().getString(R.string.email_id)).withIcon(getResources().getDrawable(R.drawable.profile))
                )
                .withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
                    @Override
                    public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
                        //Any activity or Intent
                    }
                })
                .build();
        //Create the drawer and remember the `Drawer` result object
        Drawer result = new DrawerBuilder()
                .withActivity(this)
                .withAccountHeader(headerResult)
                .withToolbar(toolbar)
                .addDrawerItems(
                        item1, item2, item3, item4, item5, item6,
                        new SectionDrawerItem().withName("Extras"),
                        item7, item8, item9, item10
                )

                //Set onClick options for drawer item click
                .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                    @Override
                    public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                        // do something with the clicked item :D
                        switch (position) {
                            //Home
                            case 1: {
                                break;
                            }
                            //Login
                            case 2: {

                               //Login Activity
                                break;
                            }

                            case 3: {
                                //Third activity
                                break;
                            }

                            case 4: {
                                break;
                            }

                            case 5: {

                                break;

                            }
                            //Location
                            case 6: {

                                break;
                            }
                            //About us
                            case 8: {
                               break;
                            }
                            //Contact Us
                            case 9: {

                                break;
                            }
                            //Feedback
                            case 10: {

                                break;
                            }
                            //Settings
                            case 11:{

                                break;
                            }

                        }
                        return true;
                    }
                })
                .build();

        result.addStickyFooterItem(new PrimaryDrawerItem().withName("Visit us again")); //Adding footer to nav drawer
    }

您必须将以下行添加到android studio

中的build.gradle文件中
compile('com.mikepenz:materialdrawer:5.3.3@aar') {
    transitive = true
}
compile 'com.mikepenz:fontawesome-typeface:4.6.0.2@aar'