2017-01-01 73 views
-3

由於我對Android非常陌生,因此我對Android中使用佈局的標準方式感到困惑。基本上,我是一名iOS開發人員。我已經使用NavigationDrawerView模板創建了一個新項目。現在我必須用導航視圖的工具欄和抽屜創建另一個活動。基本上,我想設計一個具有工具欄,抽屜和導航視圖的佈局,它可以包含在一個活動中,並且內容可以在新創建的活動中進行設計。任何人都可以提出最好的方法來做到這一點。我希望每個人都知道導航抽屜視圖的默認模板設計。所以我不在這裏包含代碼。如果有人想看代碼,請告訴我。重複使用抽屜和導航視圖的工具欄佈局

+0

可以使用片段,用片段之間的主要活動框架和交換機您的需求,以便您可以在單個活動中根據需要在片段之間切換。每個視圖都很容易訪問 –

回答

1

對於您必須使用以下XML文件: content_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/content_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="com.android.MainActivity"//you main activity 
    tools:showIn="@layout/app_bar_main"> 

</RelativeLayout> 

app_bar_main.xml:

<RelativeLayout 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:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:fitsSystemWindows="true" 
    android:orientation="vertical"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="@drawable/actionbar" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

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

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

</RelativeLayout> 

activity_main:

<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:background="@color/navDrawerbg" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_main" 
     app:menu="@menu/activity_main_drawer" /> 

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

,使主要的Java文件即mainactivity extends AppcompatActivity and implements NavigationView.OnNavigationItemSelectedListener

聲明全局varible:

private Context mContext; 
     private static FragmentManager mManager; 
       Fragment fragment = null; 

的OnCreate後創建以下方法

私人無效initUI(){

if (fragment != null) { 

     FragmentManager fragmentManager = getSupportFragmentManager(); 
     fragmentManager.beginTransaction().replace(R.id.content_main, fragment).commit(); 

    } 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    getSupportActionBar().setTitle("Home"); 

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

}

Intiate的fragmentview

private void initiateFragmentView() { 
     if (fragment != null) { 
      FragmentManager fragmentManager = getSupportFragmentManager(); 
      fragmentManager.beginTransaction() 
        .replace(R.id.content_main, fragment).commit(); 
     } 
    } 

而且在onCreate方法調用上述方法的setContentView()

mManager = getSupportFragmentManager(); 
       fragment = new Home(); 

       mContext = this; 
       initUI(); 

後打電話給您的片段時,抽屜itemselected

@Override 
     public boolean onNavigationItemSelected(MenuItem item) { 
      // Handle navigation view item clicks here. 
      int id = item.getItemId(); 

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

       //Your fragment 
       fragment = new Home(); 
       initiateFragmentView(); 

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

       fragment = new xyz(); 
       initiateFragmentView(); 


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

       fragment = new abc(); 
       initiateFragmentView(); 

      } 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
      drawer.closeDrawer(GravityCompat.START); 
      return true; 
     } 
相關問題