Android 之NavigationView&Toolbar&DrawerLayout

gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.0"
    defaultConfig {
        applicationId "com.example.jony.aircondemo1"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:design:26.+'


}

注:包的版本需要注意,design版本需要高于appcompat版本
header_layout.xml




    


两个menu文件
toolbar.xml




    


nav_menu.xml




    
        
        
        
        
        

    


注:需要在主题样式中去掉系统的ActionBar
styles.xml



    
    



main界面布局




    
    

        



        

        

java源码

package com.example.jony.aircondemo1;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,NavigationView.OnNavigationItemSelectedListener{

    private static final String TAG = "MainActivity";

    private Button openAircon;
    private DrawerLayout mDrawerlayout;
    private NavigationView navigationView;



    //加载菜单布局,以及添加事件
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        return super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.toolbar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
//        return super.onOptionsItemSelected(item);

        switch (item.getItemId()){
            case android.R.id.home:
                mDrawerlayout.openDrawer(GravityCompat.START);
                break;
            case R.id.settings:
                Toast.makeText(this, "You Clicked Settings", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }

        return true;
    }

    //监听navigationView事件
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.nav_call:
                Toast.makeText(this, "You Clicked call", Toast.LENGTH_SHORT).show();
                break;

            default:
//                Log.d(TAG, "onNavigationItemSelected: ");
                Toast.makeText(this, "You Clicked menu", Toast.LENGTH_SHORT).show();
                break;
        }

        return true;
    }

    //打开相关Activity
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.open_aircon:
                Intent openAirconIntent = new Intent();
                openAirconIntent.setClass(this, AirconActivity.class);
                startActivity(openAirconIntent);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        //DrawerLayout
        mDrawerlayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBar actionbar = getSupportActionBar();
        if (actionbar != null) {
            actionbar.setDisplayHomeAsUpEnabled(true);
            actionbar.setHomeAsUpIndicator(R.drawable.th_asphalt);
        }

        //navigation
        navigationView = (NavigationView) findViewById(R.id.navigation_view);
//        navigationView.setCheckedItem(R.id.nav_call); //默认第一个选中
//        navigationView.setOnClickListener(this); //设置监听
        navigationView.setNavigationItemSelectedListener(this);


        //Open Aircon
        openAircon = (Button) findViewById(R.id.open_aircon);
        openAircon.setOnClickListener(this);
    }


}

你可能感兴趣的:(Android 之NavigationView&Toolbar&DrawerLayout)