2016-07-13 9 views
0

Ich entwickle Android App und ich habe eine Navigationsschublade. Meine Frage ist, anstatt den gleichen Code auf jeder Seite zu schreiben (Da ich Aktivitäten nicht Fragmente verwende) muss ich es irgendwo in der Layoutdatei haben und es in andere Dateien einbinden. Wie: habe ich eine Layout-Datei für Navigationsleiste wie folgt aus:Include android.support.design.widget.NavigationView in Layout-Datei

<android.support.design.widget.NavigationView 
android:id="@+id/navigation_view" 
android:layout_height="match_parent" 
android:layout_width="match_parent" 
android:layout_gravity="start" 
app:headerLayout="@layout/nav_header" 
app:menu="@menu/drawer_view" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:app="http://schemas.android.com/apk/res/android" 
xmlns:android="http://schemas.android.com/tools" /> 

btw zeigt sie einen Fehler für die Definition Layout Höhe und Breite.

Und ich brauche in anderen Layout-Dateien wie diese gleich sind:

<include layout="@layout/navigation_view" /> 

Wie dies zu tun?

Antwort

0

Sie können BaseActivity erstellen und andere Aktivitäten daraus erweitern. Diese Idee wurde in der App iosched von Google verwendet. Es verwendet nicht NavigationView, aber Sie können die Hauptidee über die Hierarchie der Aktivität bekommen.

Grundsätzlich sollte das Layout für BaseActivity aussehen

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

<include layout="@layout/content_main" /> 

<android.support.design.widget.NavigationView 
    android:id="@+id/left_drawer" 
    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> 

Natürlich können Sie appbar hinzufügen, wenn Sie benötigen.

0

Fügen Sie in Ihrer XML-Datei ein FrameLayout ein, z.

<FrameLayout 
      android:id="@+id/myView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"/> 

Wie Sie Fragmente verwenden so in Ihrer Aktivitätsdatei:

Importe hinzufügen:

import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentTransaction; 

Und Code hinzu:

FragmentManager mFragmentManager; 
FragmentTransaction mFragmentTransaction; 

mFragmentManager = getSupportFragmentManager(); 
mFragmentTransaction = mFragmentManager.beginTransaction(); 

und schließlich Ihre FrameLayout mit der ersetzen Fragment:

mFragmentTransaction.replace(R.id.myView, new Fragment()).commit(); 
Verwandte Themen