您的位置:首页 > 移动开发 > Android开发

Android中级:ActionBar + DrawerLayout实现侧滑菜单

2016-08-20 16:15 531 查看

相关介绍

图:



有关的类:

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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: