Android中级:ActionBar + DrawerLayout实现侧滑菜单
来源:互联网 发布:动点网络 编辑:程序博客网 时间:2024/04/29 19:12
相关介绍
图:
有关的类:
- ActionBar: V7
- DrawerLayout:v4
- ActionBarDrawerToggle:v4
步骤:
- 获取ActionBar + DrawerLayout控件
- 设置ActionBar
- 创建ActionBarDrawerToggle对象,并同步
- 给添加DrawerLayout监听
代码:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout android:id="@+id/drawerLayout" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.cqc.drawerlayoutdemo1.MainActivity"> <!--内容页 要放在上面--> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:text="内容页" android:textSize="25sp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="start" android:background="#ffffff"> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:text="侧拉页" android:textSize="25sp"/> </LinearLayout></android.support.v4.widget.DrawerLayout>
MainActivity.java
package com.cqc.drawerlayoutdemo1;import android.os.Bundle;import android.support.v4.app.ActionBarDrawerToggle;import android.support.v4.widget.DrawerLayout;import android.support.v7.app.ActionBar;import android.support.v7.app.ActionBarActivity;import android.view.View;import android.view.Menu;import android.view.MenuItem;public class MainActivity extends ActionBarActivity { private DrawerLayout drawerLayout; private ActionBarDrawerToggle toggle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initActinBar(); } private void initView() { drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); } /** * 有关的类:ActionBar + ActionBarDrawerToggle + DrawerLayout */ private void initActinBar() { //1 获取ActionBar对象并设置属性 ActionBar actionBar = getSupportActionBar(); actionBar.setLogo(R.mipmap.ic_launcher);//没有图标// actionBar.setIcon(R.mipmap.ic_launcher);//图标 actionBar.setTitle(getString(R.string.app_name));// 设置标题 actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setDisplayShowHomeEnabled(true);// actionBar.setIcon(R.drawable.ic_drawer_am); //2 创建对象,ActionBarDrawerToggle设置导航图标,并同步 toggle = new ActionBarDrawerToggle(this,drawerLayout,R.drawable.ic_drawer_am,0,0); toggle.syncState(); //给drawerlayout添加监听 //方法一: drawerLayout.setDrawerListener(toggle); //方法二:// drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {// @Override// public void onDrawerSlide(View drawerView, float slideOffset) {// toggle.onDrawerSlide(drawerView,slideOffset);// }//// @Override// public void onDrawerOpened(View drawerView) {// toggle.onDrawerOpened(drawerView);// }//// @Override// public void onDrawerClosed(View drawerView) {// toggle.onDrawerOpened(drawerView);// }//// @Override// public void onDrawerStateChanged(int newState) {// toggle.onDrawerStateChanged(newState);// }// }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } //若要侧拉栏出现,还需要添加下面一行代码 @Override public boolean onOptionsItemSelected(MenuItem item) { toggle.onOptionsItemSelected(item); // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}
0 0
- Android中级:ActionBar + DrawerLayout实现侧滑菜单
- DrawerLayout和ActionBar实现侧滑菜单和Tab页
- android 使用drawerLayout实现侧滑菜单
- android 使用DrawerLayout实现侧滑菜单
- android DrawerLayout侧滑菜单实现
- Android DrawerLayout简单实现侧滑菜单
- Android利用DrawerLayout实现侧滑菜单
- DrawerLayout实现侧滑菜单
- ActionBar与DrawerLayout侧滑菜单的使用
- android 侧滑菜单DrawerLayout
- Android侧滑菜单DrawerLayout
- Android DrawerLayout-侧滑菜单
- Android DrawerLayout 侧滑菜单
- Android侧滑菜单-DrawerLayout
- Android侧滑菜单DrawerLayout
- Android侧滑菜单DrawerLayout
- Android侧滑菜单DrawerLayout(抽屉布局)实现
- android DrawerLayout实现仿QQ侧滑菜单
- VAssist X+VS2010快捷键
- Spring MVC之@RequestMapping 详解
- 快速给View添加ScrollView
- 排序——选择排序
- Reservoir sampling(水塘抽样)
- Android中级:ActionBar + DrawerLayout实现侧滑菜单
- mysql 查询缓存
- 多线程——用Lock(锁)和Condition(监听器)来优化生产者消费者模式
- PostGreSQL-9.1以后引入扩展模块
- Qt 基本数据类型转换(int,float,double,datetime,string)
- C++中相对路径与绝对路径以及斜杠与反斜杠的区别
- Decorator装饰设计模式(结构型)
- bootstrap网格系统
- Maven官方指南