Android Studio布局模板二之Navigation Menu
2016-03-29 13:48
676 查看
昨天晚上敲到快凌晨一点的时候,把这个模板的导航栏敲出来的。
添加 compile 'com.android.support:design:23.2.1'
关于布局文件层次结构:
代码较琐,贴一下较为主要的:
效果:
源码 :http://download.csdn.net/detail/csdnhejingzhou/9475420
添加 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; } }
效果:
源码 :http://download.csdn.net/detail/csdnhejingzhou/9475420
相关文章推荐
- 关于Android中Toast使用小结
- Android 绘图XML——Selector
- Android base-adapter-helper 源码分析与扩展
- Android中利用ContentResolver获取本地音乐和相片
- Android 绘图XML——layer-list
- Android 开发规范1.0
- 点击背景颜色效果
- Android 活动的四种启动模式
- Android Studio iCCP: Not recognizing known sRGB profile
- Android属性动画
- 如何控制Android控件的样式
- Android中的回调
- android基础学习之动画基础
- Android自定义标题时出现You cannot combine custom titles with other title features
- 【Android应用开发】 OpenGL ES -- 透视投影 和 正交投影
- Android Studio 添加注释模板
- android的task任务栈
- android 沉浸模式(状态栏透明 跟随布局颜色)
- Android 播放视频(三)媒体支持格式
- android中.9.png的认识