簡體   English   中英

在Android的導航抽屜中創建子菜單

[英]Creating the Sub Menu in Navigation Drawer in Android

我使用下面的鏈接創建了導航抽屜,它非常好:

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

您可以從此URl下載導航抽屜-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下載完整的Navigation Working代碼,如上所述。

這是我想要的照片 在此處輸入圖片說明

您應該使用Android支持設計庫中的NavigationView代替此NavigationDrawer。

檢查此官方樣本:

https://github.com/chrisbanes/cheesesquare

這很容易。

使用Android Suppor設計庫,您將如下創建子菜單:

<?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>

最好的祝福。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM