java - 在 Android 的抽屉导航中创建子菜单

标签 java android

我使用下面的链接创建了抽屉导航,这非常好:

http://blog.teamtreehouse.com/add-navigation-drawer-android

您可以从这个 URl 下载 Navigation Drawer - https://github.com/sanjaisy/Android-Navigation-Drawer.git

现在我想将子菜单添加到这个抽屉导航中。请帮我解决。

这是我的完整java代码

public class SmoothBanlanceHome extends ActionBarActivity {

        private ListView mDrawerList;
        private DrawerLayout mDrawerLayout;
        private ArrayAdapter<String> mAdapter;
        private ActionBarDrawerToggle mDrawerToggle;
        private String mActivityTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smooth_banlance_home);

        mDrawerList = (ListView)findViewById(R.id.navList);mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        mActivityTitle = getTitle().toString();

        addDrawerItems();
        setupDrawer();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
    }

        private void addDrawerItems() {
            String[] MenuArray = getResources().getStringArray(R.array.Naviagation_Menu_List);
            mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, MenuArray);
            mDrawerList.setAdapter(mAdapter);

            mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(SmoothBanlanceHome.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
                }
            });
        }

        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("Menu");
                    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);
        }

        @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            // Sync the toggle state after onRestoreInstanceState has occurred.
            mDrawerToggle.syncState();
        }

        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            mDrawerToggle.onConfigurationChanged(newConfig);
        }}

这是布局代码

 <!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- android:layout_gravity="start" tells DrawerLayout to treat
         this as a sliding drawer on the left side for left-to-right
         languages and on the right side for right-to-left languages.
         The drawer is given a fixed width in dp and extends the full height of
         the container. A solid background is used for contrast
         with the content view. -->
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

谁能告诉我如何使用这段代码制作子菜单。您还可以从我上面提到的 git url 下载完整的导航工作代码。

这是我想要的图片 enter image description here

最佳答案

您应该使用 Android 支持设计库中的 NavigationView 而不是此 NavigationDrawer。

检查这个官方示例:

https://github.com/chrisbanes/cheesesquare

这很容易。

使用 Android 支持设计库,您将创建如下子菜单:

<?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_home"
            android:icon="@drawable/ic_event"
            android:title="Home" />
        <item
            android:id="@+id/nav_profile"
            android:icon="@drawable/ic_dashboard"
            android:title="Perfil" />
    </group>

    <item android:title="More Options">
        <menu>
            <item
                android:icon="@drawable/ic_forum"
                android:title="Forum" />
            <item
                android:icon="@drawable/ic_headset"
                android:title="Headset" />
        </menu>
    </item>
</menu>

最好的问候。

关于java - 在 Android 的抽屉导航中创建子菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31776131/

相关文章:

java - EL 实现中缺少资源 : ? ??propertyNotReadable?

android - 带有 eq ("string"的白盒 invokeMethod )

android - 在Android中实现双击按钮

java - 带有 Cookie 的 Android MediaPlayer URL

android - 如何处理显然相互覆盖的gradle依赖项

java - 全局配置应该使用什么设计模式

java - 线程卡在 getDeclaredConstructors 中的可能原因?

java - 在 @RequestMapping 的值中使用或不使用前导斜线。需要官方文档或指向 Spring 源代码?

java - 使用 sql-maven-plugin 时 <outputFile> 属性无法正常工作

java - 安卓工作室 : Error Code 1: Gradle: Execution failed for task ':app:processDebugResources'