Android Studio布局模板二之Navigation Menu

昨天晚上敲到快凌晨一点的时候,把这个模板的导航栏敲出来的。


添加 compile 'com.android.support:design:23.2.1'

关于布局文件层次结构:



代码较琐,贴一下较为主要的:

一、activity_layout.xml

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


二、MainActivity.java

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

}


效果:
                

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值