如何在导航栏中的项目之间添加边框?

时间:2017-11-04 18:29:58

标签: android navigation-drawer android-navigation-drawer

我正在尝试在导航抽屉上添加两个不同项目之间的边框。 我已经将 Mikepenz Material Drawer https://github.com/mikepenz/MaterialDrawer)用于Naviagation抽屉。

这是截图。我只想要导航抽屉中的边界线,我不希望这个标题像“下载器”和“分享和评价”

在这里看一下截图:

screen

我在主要活动中添加了这一行,以区分下载者和其他人:   new SectionDrawerItem()。withName(R.string.drawer_item_section_header),

这是导航抽屉的java代码:

//Navigation Drawer
    headerResult = new AccountHeaderBuilder()
            .withActivity(this)
            .withHeaderBackground(R.color.colorPrimary)
            .withSelectionListEnabledForSingleProfile(false)
            .withAlternativeProfileHeaderSwitching(false)
            .withCompactStyle(false)
            .withDividerBelowHeader(false)
            .withProfileImagesVisible(true)
            .withTypeface(typeface)
            .addProfiles(new ProfileDrawerItem().withIcon(R.mipmap.ic_launcher).withName(getResources().getString(R.string.app_name)).withEmail(getResources()
                    .getString(R.string.developer_email)))
            .build();
    resultDrawer = new DrawerBuilder()
            .withActivity(this)
            .withSelectedItem(-1)
            .withFullscreen(true)
            .withAccountHeader(headerResult)
            .withActionBarDrawerToggle(true)
            .withCloseOnClick(true)
            .withMultiSelect(false)
            .withTranslucentStatusBar(true)
            .withToolbar(mToolbar)
            .addDrawerItems(
                    new PrimaryDrawerItem().withSelectable(false).withName(R.string.app_name).withTypeface(typeface),
                    new PrimaryDrawerItem().withSelectable(false).withName("Downloads").withIcon(R.drawable.ic_inst).withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                        @Override
                        public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                            if (placeholderLayout.getVisibility() == View.VISIBLE) {
                                placeholderLayout.setVisibility(View.GONE);
                                recyclerLayout.setVisibility(View.VISIBLE);
                                setUpRecyclerView();
                            }
                            return false;
                        }
                    }).withTypeface(typeface),


                    new SectionDrawerItem().withName(R.string.drawer_item_section_header),


                    new PrimaryDrawerItem().withSelectable(false).withName("Story Downloader").withIcon(R.drawable.ic_whatshot_black_24dp).withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                        @Override
                        public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                            Intent intent = new Intent(MainActivity.this,SplashActivity.class);
                            startActivity(intent);
                            return false;
                        }
                    }).withTypeface(typeface),
                    new PrimaryDrawerItem().withSelectable(false).withName("WhatsApp Downloader").withIcon(R.drawable.ic_wapp).withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                        @Override
                        public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                            Intent intent = new Intent(MainActivity.this,WaActivity.class);
                            startActivity(intent);
                            return false;
                        }
                    }).withTypeface(typeface),
                    new PrimaryDrawerItem().withSelectable(false).withName("Twitter Downloader").withIcon(R.drawable.ic_tweet).withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                        @Override
                        public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                            Intent intent = new Intent(MainActivity.this,TweetActivity.class);
                            startActivity(intent);
                            return false;
                        }
                    }).withTypeface(typeface),
                    new PrimaryDrawerItem().withSelectable(false).withName("Facebook Downloader").withIcon(R.drawable.ic_facebook_logo).withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                        @Override
                        public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                            Intent intent = new Intent(MainActivity.this,FbActivity.class);
                            startActivity(intent);
                            return false;
                        }
                    }).withTypeface(typeface),

                    new SectionDrawerItem().withName(R.string.drawer_item_section_share),

                    new PrimaryDrawerItem().withSelectable(false).withName("Recommend to Friends").withIcon(R.drawable.ic_share_black_24dp).withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                        @Override
                        public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                            final String shareappPackageName = getPackageName();
                            Intent sendIntent = new Intent();
                            sendIntent.setAction(Intent.ACTION_SEND);
                            sendIntent.putExtra(Intent.EXTRA_TEXT, "Check out Social Downloader App at: https://play.google.com/store/apps/details?id=" + shareappPackageName);
                            sendIntent.setType("text/plain");
                            startActivity(sendIntent);
                            return false;
                        }
                    }).withTypeface(typeface),
                    new PrimaryDrawerItem().withSelectable(false).withName("Rate Us").withIcon(R.drawable.ic_thumb_up_black_24dp).withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                        @Override
                        public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                            final String appPackageName = getPackageName();
                            try {
                                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
                            } catch (ActivityNotFoundException anfe) {
                                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
                            }
                            return false;
                        }
                    }).withTypeface(typeface),



                    new PrimaryDrawerItem().withSelectable(false).withName("Help").withIcon(R.drawable.ic_help_black_24dp).withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                        @Override
                        public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                            Intent intent = new Intent(MainActivity.this, IntroActivity.class);
                            startActivity(intent);
                            return false;
                        }
                    }).withTypeface(typeface),


                    new PrimaryDrawerItem().withSelectable(false).withName("Privacy Policy").withIcon(R.drawable.ic_info_black_24dp).withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                        @Override
                        public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                            AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
                                    .setTitle("PRIVACY POLICY")
                                    .setMessage(R.string.privacy_message)
                                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.dismiss();
                                        }
                                    })
                                    .setIcon(R.drawable.ic_info_black_24dp)
                                    .show();
                            TextView textView = (TextView) dialog.findViewById(android.R.id.message);
                            textView.setScroller(new Scroller(MainActivity.this));
                            textView.setVerticalScrollBarEnabled(true);
                            textView.setMovementMethod(new ScrollingMovementMethod());
                            return false;
                        }
                    }).withTypeface(typeface),
                    new PrimaryDrawerItem().withSelectable(false).withName("Feedback").withIcon(R.drawable.ic_feedback_black_24dp).withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                        @Override
                        public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                            DisplayMetrics displaymetrics = new DisplayMetrics();
                            getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
                            int height = displaymetrics.heightPixels;
                            int width = displaymetrics.widthPixels;
                            PackageManager manager = getApplicationContext().getPackageManager();
                            PackageInfo info = null;
                            try {
                                info = manager.getPackageInfo(getPackageName(), 0);
                            } catch (PackageManager.NameNotFoundException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            String version = info.versionName;
                            Intent i = new Intent(Intent.ACTION_SEND);
                            i.setType("message/rfc822");
                            i.putExtra(Intent.EXTRA_EMAIL, new String[]{getResources().getString(R.string.developer_email)});
                            i.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.app_name) + version);
                            i.putExtra(Intent.EXTRA_TEXT,
                                    "\n" + " Device :" + getDeviceName() +
                                            "\n" + " System Version:" + Build.VERSION.SDK_INT +
                                            "\n" + " Display Height  :" + height + "px" +
                                            "\n" + " Display Width  :" + width + "px" +
                                            "\n\n" + "Have a problem? Please share it with us and we will do our best to solve it!" +
                                            "\n");
                            startActivity(Intent.createChooser(i, "Send Email"));
                            return false;
                        }
                    }).withTypeface(typeface),
                    new PrimaryDrawerItem().withSelectable(false).withName("Exit").withIcon(R.drawable.ic_exit_to_app_black_24dp).withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                        @Override
                        public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                            finish();
                            return false;
                        }
                    }).withTypeface(typeface)
            ).
                    withSavedInstance(savedInstanceState)
            .build();
    loadInterstitialAd();
}

2 个答案:

答案 0 :(得分:0)

通常您需要在菜单xml文件中执行此操作: 只需将每个菜单项放在不同的组中,每个组必须有一个id(每个组由分隔符分隔)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group
        android:id="@+id/item1">
        <item
            android:id="@+id/nav_settings"
            android:icon="@drawable/ic_settings"
            android:title="Settings"/>
    </group>
<group
        android:id="@+id/item2">
        <item
            android:id="@+id/nav_info"
            android:icon="@drawable/ic_info"
            android:title="Info"/>
    </group>
</menu>

<强>更新

我刚检查了你使用的抽屉的github。我不熟悉它,但我注意到了一种方法:

result.addItem(new DividerDrawerItem());

也许它会有所帮助,抱歉,我无能为力,但您可能会在文档和源代码示例中找到所需的一切

答案 1 :(得分:0)

请确保为每个组指定一个ID,它就像魔术一样工作

  <group android:checkableBehavior="single"
           android:id="@+id/group1">

        <item
            android:id="@+id/nav_about"
            android:icon="@drawable/ic_about"
            android:title="About" />

    </group>

    <group android:checkableBehavior="single"
           android:id="@+id/group2">

            <item
                android:id="@+id/nav_settings"
                android:icon="@drawable/ic_settings"
                android:title="Settings" />
    </group>