简体   繁体   English

Android Studio 1.4中的导航抽屉模板

[英]Navigation drawer template in Android Studio 1.4

I use the Navigation drawer template on Android Studio 1.4 (the one with activity_main.xml , app_bar_main.xml , content_main.xml , nav_header_main.xml ) 我在Android Studio 1.4上使用了导航抽屉模板(一个带有activity_main.xmlapp_bar_main.xmlcontent_main.xmlnav_header_main.xml

I was able to change it (icons, methods,...) to feed my needs, 我能够更改它(图标,方法等)以满足我的需求,

The problem is that I want all my activities to have the same navigation drawer, so in each Java file of my activities, I extend the activity containing the navigation drawer. 问题是我希望所有活动都具有相同的导航抽屉,因此在活动的每个Java文件中,我都扩展了包含导航抽屉的活动。 It didn't work. 没用

So tried to I include the layout of the activity which contains the navigation drawer. 因此,我尝试包含包含导航抽屉的活动的布局。 And it's not working too. 而且它也不起作用。

Can you give me some suggestions how to make it work? 您能给我一些建议如何使其工作吗?

Here the java and xml files: 这里的java和xml文件:

1- mainactivity.java 1- mainactivity.java

package com.ther01s.android.wabamon;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class Rubriques extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rubriques);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

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

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

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.rubriques, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_categories) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

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

        if (id == R.id.nav_camera) { profil();
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

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

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

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

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

        }

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

    private void profil() { Intent intent = new Intent(this, Profil.class);
        startActivity(intent);
    }
}

2-mainactivity.xml 2- mainactivity.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout
    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="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.ther01s.android.wabamon.Rubriques">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        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:orientation="vertical"
        >



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

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

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


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

    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_rubriques"
        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_rubriques"
        app:menu="@menu/activity_rubriques_drawer" />

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

</LinearLayout>

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

3-firstactivity.java 3-firstactivity.java

package com.ther01s.android.wabamon;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.View;

public class Catalogue extends Rubriques {


    private DrawerLayout mDrawer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LayoutInflater inflater = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView = inflater.inflate(R.layout.activity_catalogue, null, false);


        mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        mDrawer.addView(contentView, 0);


    }

}

4-firstactivity.xml 4- firstactivity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.ther01s.android.wabamon.Catalogue">



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



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

The best solution would be to use only one Activity. 最好的解决方案是仅使用一个Activity。 All your content would be in Fragments. 您所有的内容都将在“片段”中。 When the user selects an item in the navigation drawer, the Fragment displayed by the Activity is swapped out. 当用户在导航抽屉中选择一个项目时,“活动”显示的“片段”将换出。 This way you'll also have a smooth animation for the drawer, as the Activity containing the drawer is not finished. 这样,您还将为抽屉创建一个平滑的动画,因为包含抽屉的活动尚未完成。

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

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