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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical">
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/th_asphalt"/>
</LinearLayout>
两个menu文件
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/settings"
android:title="Settings"
app:showAsAction="always"/>
</menu>
nav_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_call"
android:title="Call" />
<item
android:id="@+id/nav_friends"
android:title="Friends" />
<item
android:id="@+id/nav_location"
android:title="Location" />
<item
android:id="@+id/nav_mail"
android:title="Mail" />
<item
android:id="@+id/nav_task"
android:title="Task" />
</group>
</menu>
注:需要在主题样式中去掉系统的ActionBar
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
main界面布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--主屏幕-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toobar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
<Button
android:id="@+id/open_aircon"
android:layout_width="120dp"
android:layout_height="120dp"
android:textAllCaps="false"
android:layout_gravity="bottom"
android:text="@string/aircon_name"
/>
</FrameLayout>
<!--滑动菜单-->
<!--<TextView-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:layout_gravity="start"-->
<!--android:text="This is menu"-->
<!--android:textSize="30sp"-->
<!--android:background="#FFF"/>-->
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
android:fitsSystemWindows="true"
app:headerLayout="@layout/header_layout"
app:menu="@menu/nav_menu">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
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);
}
}
网友评论