菜单项不会出现在导航栏中

时间:2016-04-26 10:22:37

标签: android android-layout navigation-drawer

我坚持这件事:我试图创建导航抽屉的布局。不幸的是,即使标题恰好位于正确的位置,我也无法显示菜单中的项目。我将提供我的.xml布局文件。拜托,帮帮我吧!

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
    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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <android.support.design.widget.NavigationView
        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:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/menu_header"
        app:menu="@menu/new_menu">

    </android.support.design.widget.NavigationView>


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

MainActivity代码

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity implements NavigationView.OnNavigationItemSelectedListener{
private String[] mMenuChoices;
private ListView mDrawerList;

private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;

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

    mMenuChoices = getResources().getStringArray(R.array.menu_labels);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.nav_view);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
     fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    mTitle = mDrawerTitle = getTitle();
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };

    // Set the drawer toggle as the DrawerListener
    mDrawerLayout.addDrawerListener(mDrawerToggle);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

}

@Override
public void onBackPressed(){
    DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    if(drawerLayout.isDrawerOpen(GravityCompat.START)){
        drawerLayout.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item){
    int id = item.getItemId();

    if(id == R.id.nav_newsfeed){

    } else if (id == R.id.nav_appwall){

    } else if (id == R.id.nav_appwall){

    } else if (id == R.id.nav_appwall){

    } else if (id == R.id.nav_appwall){

    } else if (id == R.id.nav_appwall){

    } else if (id == R.id.nav_appwall){

    } else if (id == R.id.nav_appwall){

    } else if (id == R.id.nav_appwall){

    }

    DrawerLayout drawerLayout =(DrawerLayout) findViewById(R.id.drawer_layout);
    drawerLayout.closeDrawer(GravityCompat.START);
    return true;

}

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


@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

@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);
}

@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);
}


}

菜单代码

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group
        android:id="@+id/group_activities"
        android:checkableBehavior="single">
        <item
            android:id="@+id/label"
            android:checked="true"
            android:icon="@mipmap/label"
            android:title="@string/label" />
        <item
            android:id="@+id/label"
            android:checked="true"
            android:icon="@mipmap/label"
            android:title="@string/label" />
        <item
            android:id="@+id/label"
            android:checked="true"
            android:icon="@mipmap/label"
            android:title="@string/label" />
        <item
            android:id="@+id/label"
            android:checked="true"
            android:icon="@mipmap/label"
            android:title="@string/label" />
        <item
            android:id="@+id/label"
            android:checked="true"
            android:icon="@mipmap/label"
            android:title="@string/label" />
        <item
            android:id="@+id/label"
            android:checked="true"
            android:icon="@mipmap/label"
            android:title="@string/label" />
        <item
            android:id="@+id/label"
            android:checked="true"
            android:icon="@mipmap/label"
            android:title="@string/label" />
        <item
            android:id="@+id/label"
            android:checked="true"
            android:icon="@mipmap/label"
            android:title="@string/label" />
    </group>

    <group
        android:id="@+id/group_sign_out"
        android:checkableBehavior="single">
        <item
            android:id="@+id/nav_sign_out"
            android:icon="@mipmap/ic_sign_out"
            android:title="@string/label_sign_out" />
    </group>

</menu>

我刚刚删除了ID,图标和标题的名称。但它们正确显示在我的菜单文件中。不幸的是,当我打开预览的设计小部件时,我的菜单并没有显示出来。

2 个答案:

答案 0 :(得分:0)

添加最后一行,如。

mMenuChoices = getResources().getStringArray(R.array.menu_labels);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.nav_view);
// Set the adapter for the list view
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list_item, mMenuChoices));

答案 1 :(得分:0)

使用新的布局渲染引擎。 为此,请转到设置 ? 实验 ? 选中使用新的布局渲染引擎。