赞
踩
昨天晚上敲到快凌晨一点的时候,把这个模板的导航栏敲出来的。
添加 compile 'com.android.support:design:23.2.1'
关于布局文件层次结构:
代码较琐,贴一下较为主要的:
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:id="@+id/drawerLayout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:fitsSystemWindows="true"
- tools:openDrawer="start"
- tools:context="com.example.hejingzhou.navigationdemo.MainActivity">
-
- <include
- layout="@layout/toolbar_layout"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
-
- <android.support.design.widget.NavigationView
- android:background="#778899"
- android:id="@+id/navigationView"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:layout_gravity = "start"
- android:fitsSystemWindows="true"
- app:headerLayout="@layout/header_layout"
- app:menu="@menu/self_menu"/>
-
- </android.support.v4.widget.DrawerLayout>

- package com.example.hejingzhou.navigationdemo;
-
- import android.content.Intent;
- 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.os.Bundle;
- import android.support.v7.widget.Toolbar;
- 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 NavigationView.OnNavigationItemSelectedListener{
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- Button TestBtn = (Button)findViewById(R.id.TestButton);
- TestBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(MainActivity.this, "点击了测试Button", Toast.LENGTH_SHORT).show();
- }
- });
-
- Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
- toolbar.setTitle("贺景洲");
- setSupportActionBar(toolbar);
-
- DrawerLayout drawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
- ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(//ActionBarDrawerToggle将组件组合的一个简单的方法类
- this,drawerLayout,toolbar,R.string.open,R.string.close);
- /**
- * ActionBarDrawerToggle 构造方法
- * @param activity The Activity hosting the drawer.
- * @param toolbar The toolbar to use if you have an independent Toolbar.
- * @param drawerLayout The DrawerLayout to link to the given Activity's ActionBar
- * @param openDrawerContentDescRes A String resource to describe the "open drawer" action
- * for accessibility
- * @param closeDrawerContentDescRes A String resource to describe the "close drawer" action
- * for accessibility
- */
- drawerLayout.setDrawerListener(toggle);// 抽屉事件通知设置一个侦听器
- toggle.syncState();//抽屉的状态指示器/启示与链接DrawerLayout同步
- NavigationView navigationView = (NavigationView)findViewById(R.id.navigationView);
- navigationView.setNavigationItemSelectedListener(this);//设置一个侦听器,当单击菜单项时,将通知。
- }
-
- /**
- * 当按下返回键的时候的 如果抽屉开着将关闭抽屉
- */
- @Override
- public void onBackPressed() {
- DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawerLayout);
- if(drawer.isDrawerOpen(GravityCompat.START)){
- drawer.closeDrawer(GravityCompat.START);
- }else {
- super.onBackPressed();
- }
- //super.onBackPressed();
- }
-
- /**
- * 抽屉项的监听
- * @param item
- * @return
- */
- @Override
- public boolean onNavigationItemSelected(MenuItem item) {
-
- int id = item.getItemId();
- if(id == R.id.message){
- Toast.makeText(this,"点击了个人信息",Toast.LENGTH_SHORT).show();
- }else if(id == R.id.camera){
- Toast.makeText(this,"点击了打开相机",Toast.LENGTH_SHORT).show();
- }else if(id == R.id.joke){
- Toast.makeText(this,"点击了今日笑话",Toast.LENGTH_SHORT).show();
- }else if(id == R.id.phto){
- Toast.makeText(this,"点击了我的图库",Toast.LENGTH_SHORT).show();
- }else if(id == R.id.setNet){
- Toast.makeText(this,"点击了网络设置",Toast.LENGTH_SHORT).show();
- }else if(id == R.id.setbluetools){
- Toast.makeText(this,"点击了蓝牙设置",Toast.LENGTH_SHORT).show();
- }
-
- DrawerLayout drawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
- drawerLayout.closeDrawer(GravityCompat.START);
- return true;
- }
-
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。