【问题标题】:Using navigationview, but hamburger menu does not work使用导航视图,但汉堡菜单不起作用
【发布时间】:2016-02-14 15:50:07
【问题描述】:

大家!我目前正在尝试使用导航视图来创建导航抽屉,下面是代码

AndroidMainfest

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>
    <activity
        android:name=".C"
        android:label="@string/title_activity_c"
        android:parentActivityName=".MainActivity"
        android:theme="@style/AppTheme" >

    </activity>
    <activity
        android:name=".New_Request"
        android:label="@string/title_activity_new__request" >
    </activity>
</application>

C.java

public class C extends AppCompatActivity {
DrawerLayout drawerLayout;

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

    setupDrawerLayout();

    // my_child_toolbar is defined in the layout file
    Toolbar myChildToolbar = (Toolbar) findViewById(R.id.my_childtoolbar);
    setSupportActionBar(myChildToolbar);

    // Get a support ActionBar corresponding to this toolbar
    ActionBar ab = getSupportActionBar();

    // Enable the Up button
    if(ab != null) {
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setHomeAsUpIndicator(R.drawable.rsz_ic_list_white_48dp);
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_c, menu);

    MenuItem searchitem = menu.findItem(R.id.action_search1);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchitem);

    // Define the listener
    MenuItemCompat.OnActionExpandListener expandListener = new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            // Do something when action item collapses
            return true;  // Return true to collapse action view
        }

        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            // Do something when expanded
            return true;  // Return true to expand action view
        }
    };

    // Get the MenuItem for the action item
    MenuItem actionMenuItem = menu.findItem(R.id.action_sharre);

    MenuItemCompat.setOnActionExpandListener(actionMenuItem, expandListener);

    // Assign the listener to that action item
    return super.onCreateOptionsMenu(menu);
}

private void setupDrawerLayout(){
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout_c);

    NavigationView view = (NavigationView) findViewById(R.id.navigation_view);
    view.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            Snackbar.make(getWindow().getDecorView(), menuItem.getTitle() + " pressed", Snackbar.LENGTH_LONG).show();
            menuItem.setChecked(true);
            drawerLayout.closeDrawers();
            return true;
        }
    });

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    switch (id){
        case R.id.home:
            drawerLayout.openDrawer(GravityCompat.START);
            Log.d("kk","reallt");

            return true;
        case R.id.action_settings:
            return true;

    }


    //noinspection SimplifiableIfStatement


    return super.onOptionsItemSelected(item);
}

}

activity_c.xml

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout_c"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content_linear"
tools:context="com.example.ji.myapplication19.C">

<android.support.v7.widget.Toolbar
    android:id="@+id/my_childtoolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="#2196F3"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</FrameLayout>

</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/drawer_head"
    app:menu="@menu/navigation_view_menu"/>

</android.support.v4.widget.DrawerLayout>

你的主要问题是:当点击汉堡菜单时,它没有出现导航抽屉。 有人帮帮我吗?

【问题讨论】:

    标签: android navigation navigation-drawer


    【解决方案1】:

    在 onCreate() 方法之前的活动类中

    ActionBarDrawerToggle mDrawerToggle;
    

    setupDrawerLayout() 中添加:

    mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, myChildToolbar, "Open", "Close") {
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }
    
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }
        };
    drawerLayout.post(new Runnable() {
            @Override
            public void run() {
                mDrawerToggle.syncState();
            }
    });
    drawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerToggle.syncState();
    

    在此之后,您还必须对 onOptionsItemSelected 进行一些更改,如下所示:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Pass the event to ActionBarDrawerToggle, if it returns
        // true, then it has handled the app icon touch event
        if (mDrawerToggle.onOptionsItemSelected(item)) {
          return true;
        }
        // Handle your other action bar items...
    
        return super.onOptionsItemSelected(item);
    }
    

    【讨论】:

    【解决方案2】:
    drawerLayout = (DrawerLayout)findViewById(R.id.drawerlayout);
    
    actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,R.string.opendrawer,R.string.closedrawer);
    
            drawerLayout.setDrawerListener(actionBarDrawerToggle);
    
            ActionBar actionBar = getSupportActionBar();
            actionBar.setDisplayShowHomeEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeButtonEnabled(true);
    

    【讨论】:

    • 我使用我选择的答案,无论如何,谢谢你的帮助
    猜你喜欢
    • 1970-01-01
    • 2022-12-18
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多