Android中级:ActionBar + DrawerLayout实现侧滑菜单
2016-08-20 16:15
531 查看
相关介绍
图:
有关的类:
ActionBar: V7DrawerLayout: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); } }
相关文章推荐
- android 应用架构随笔五(ActionBar与侧滑菜单DrawerLayout)
- Android 使用DrawerLayout快速实现侧滑菜单
- Android Toolbar+DrawerLayout+PagerSlidingTabStrip实现仿csdn侧滑菜单
- Android侧滑菜单DrawerLayout(抽屉布局)实现
- Android Material Design系列之使用Toolbar + DrawerLayout实现高侧滑菜单
- Android使用DrawerLayout实现仿QQ双向侧滑菜单
- android 使用drawerLayout实现侧滑菜单
- Android使用DrawerLayout实现抽屉式侧滑菜单
- [AndroidUI]使用DrawerLayout实现侧滑菜单
- Android 高仿QQ5.2双向侧滑菜单DrawerLayout实现源码
- android 使用DrawerLayout实现侧滑菜单
- android DrawerLayout实现仿QQ侧滑菜单
- android DrawerLayout 实现侧滑菜单 知识整理(二)
- Android:使用Toolbar + DrawerLayout快速实现菜单侧滑
- Android DrawerLayout+NavigationView布局实现左右两边侧滑菜单
- Android开发-DrawerLayout实现侧滑菜单(1)-自定义ListView的实现&沉浸式状态栏
- Android使用DrawerLayout和ToolBar实现仿知乎侧滑菜单
- Android使用DrawerLayout实现侧滑菜单效果
- Android新手轻松学知乎日报开发(四)使用Toolbar + DrawerLayout实现菜单侧滑
- 用Android自带的DrawerLayout和ActionBarDrawerToggle实现侧滑效果