简体   繁体   English

使用抽屉和导航视图重用工具栏布局

[英]Reusing toolbar layout with drawer and navigation view

As I am very new to Android, I have some confusion for standard way of using layouts in Android.由于我对 Android 非常陌生,因此我对在 Android 中使用布局的标准方式有些困惑。 Basically, I am an iOS developer.基本上,我是一名 iOS 开发人员。 I have created a new project with NavigationDrawerView template.我用 NavigationDrawerView 模板创建了一个新项目。 Now I have to create another activities with toolbar and drawer with navigation view.现在我必须创建另一个带有导航视图的工具栏和抽屉的活动。 Basically, I want to design a layout having toolbar, drawer and navigation view, which can be just included in an activity and content can be designed in the newly created activity.基本上,我想设计一个具有工具栏、抽屉和导航视图的布局,这些布局可以只包含在一个活动中,而内容可以在新创建的活动中设计。 Can anyone suggest the best way to do this.任何人都可以建议最好的方法来做到这一点。 I hope every one aware of the default template design for navigation drawer view.我希望每个人都知道导航抽屉视图的默认模板设计。 So that I am not including the code here.所以我不在这里包含代码。 If anybody wants to see the code, let me know.如果有人想看代码,请告诉我。

For that you have to use following xml files: content_main.xml:为此,您必须使用以下 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: 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>

And Make main java file ie mainactivity extends AppcompatActivity and implements NavigationView.OnNavigationItemSelectedListener并制作主java文件,即mainactivity extends AppcompatActivity and implements NavigationView.OnNavigationItemSelectedListener

Declare global varible:声明全局变量:

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

Create below method after oncreate在 oncreate 之后创建以下方法

private void 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 the fragmentview启动片段视图

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

And call above method in the oncreate method after setcontentview()并在 setcontentview() 之后的 oncreate 方法中调用上面的方法

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

                mContext = this;
                initUI();

Call your fragments when drawer 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;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM