hallo i Navigationsleiste in meinem Android-Anwendung in der Haupttätigkeit Java-Klasse haben, und dies ist der CodeNavigationsleiste Haupttätigkeit Inhalt
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//add this line to display menu1 when the activity is loaded
displaySelectedScreen(R.id.nav_menu1);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
private void displaySelectedScreen(int itemId) {
//creating fragment object
Fragment fragment = null;
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
//initializing the fragment object which is selected
switch (itemId) {
case R.id.nav_menu1:
fragment = new gallary();
break;
case R.id.nav_menu2:
fragment = new about();
break;
case R.id.nav_menu3:
fragment = new contact();
break;
}
//replacing the fragment
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
drawer.closeDrawer(GravityCompat.START);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
//calling the method displayselectedscreen and passing the id of selected menu
displaySelectedScreen(item.getItemId());
//make this method blank
return true;
}
}
und das ist die Haupttätigkeit xml Code
<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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
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/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
also möchte ich einige Inhalte wie Bilder und Texte zur Hauptaktivitäts-XML-Seite der App hinzufügen, aber wenn ich sie im Hauptinhalt der Navigation hinzufüge, sieht es gut aus und alles in Ordnung, aber wenn ich zum Beispiel auf Gallary-Fragment aus der Navigationsschublade klicke hat mich zu neuer Seite gebracht, die den Inhalt hat, den ich in die Galle gelegt habe ary xml aber das Problem ist der Inhalt der Haupttätigkeit erscheint auch mit allen Fragment Seiten, die ich aus der Navigationsschublade klicken Wie kann ich den Inhalt jedes Fragments erscheinen ohne den Inhalt der Hauptaktivität jemand mich verstehen? @ _ @
fügen Sie bitte die Layout-Dateien –
i die Frage bearbeiten und hinzufügen – mh9