Ich bin mit diesem Ding fest: Ich versuche, das Layout einer Navigationsschublade zu erstellen. Leider kann ich keine Artikel aus meinem Menü erscheinen lassen, obwohl der Header zufällig am richtigen Ort ist. Ich werde meine .xml der Layout-Datei bereitstellen. Bitte hilf mir!Menüpunkte erscheinen nicht in der Navigationsleiste
<?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 Code
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);
}
}
Menü Code
<?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>
ich Namen von id, Icons und Titel nur entfernt. Aber sie erscheinen in meiner Menü-Datei korrekt. Leider erscheint mein Menü nicht, wenn ich das Design-Widget für die Vorschau einschalte.
Bitte fügen Sie auch den Aktivitätscode hinzu. –
MainActivity-Code hinzugefügt. – Kromag
Siehe Res/Menü/Verzeichnis für Menüelemente der Navigationsleiste. – Alvin