android 5.X Toolbar+DrawerLayout实现抽屉菜单
前言
?android5.X新增的一个控件Toolbar,这个控件比ActionBar更加自由,可控,由于以前的ActionBar的灵活性比较差,所以google逐渐使用Toolbar替代ActionBar,所以Toolbar也可以说是超级ActionBar。
这篇文章不详细介绍ToolBar的使用(定制),主要是介绍Toolbar使用的一个例子,即Toolbar结合DrawerLayout实现抽屉菜单。
使用这个两个控件需要引入相应的库依赖:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
}
1. Toolbar的布局文件
include_toolbar.xml
Toolbar控件使用和其他的控件使用类似,需要注意的是引用v7扩展包中的Toolbar,而不是系统自带的那个Toolbar,这样可以兼容v21以下的设备。
2. 添加动作菜单
菜单项通常放在menu文件夹下的一个xml文件中
menu_main.xml
之前的ActionBar也是需要使用这个菜单文件来渲染其动作菜单,Toolbar也一样。
有了菜单布局文件,还需要在代码中初始化菜单,这样Toolbar上面才会显示这些动作菜单(重写Activity的onCreateOptionsMenu()
方法):
/**
* 必须重写该方法创建菜单,不然菜单不会显示在Toolbar中
* @param menu
* @return
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
3. activity主布局文件
activity_main.xml
<framelayout android:id="@+id/fl_containor" android:layout_height="match_parent" android:layout_width="match_parent">
</framelayout>
4. 代码初始化Toolbar
@NonNull
private void initToolbar() {
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mToolbar.setLogo(R.mipmap.ic_launcher);
mToolbar.setTitle("主标题");
mToolbar.setSubtitle("副标题");
// ...
// 这个方法很重要,不能少,让toolbar取代ActionBar
setSupportActionBar(mToolbar);
}
注意:一定要调用setSupportActionBar()
让Toolbar取代ActionBar,同时还需要将当前Activity的主题设置为NoActionBar。
5. 将Toolbar与DrawerLayout状态绑定
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.open, R.string.close);
drawerToggle.syncState();
mDrawerLayout.setDrawerListener(drawerToggle);
6. 初始化内容视图
private void initContentView() {
Fragment fragment = new ContentFragment();
Bundle bundle = new Bundle();
bundle.putString("title","首页");
fragment.setArguments(bundle);
mFm.beginTransaction().replace(R.id.fl_containor,fragment).commit();
}
ContentFragment.java
package com.example.lt.toolbar;
import android.app.ActionBar;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by lt on 2016/3/16.
*/
public class ContentFragment extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
TextView textView = new TextView(getActivity());
if(getArguments()!=null){
String title = getArguments().getString("title");
textView.setText(title);
textView.setGravity(Gravity.CENTER);
textView.setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
}
return textView;
}
}
7. 初始化抽屉菜单
private void initLeftMenu() {
ListView menuList = (ListView) findViewById(R.id.lv_menu);
menuList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1
, new String[]{"首页","新闻","个人中心"}));
menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
Fragment fragment = new ContentFragment();
Bundle bundle = new Bundle();
bundle.putString("title", ((TextView) view).getText().toString());
fragment.setArguments(bundle);
mFm.beginTransaction().replace(R.id.fl_containor, fragment).commit();
mDrawerLayout.closeDrawer(Gravity.LEFT);
}
});
}
这里用了一个ListView来渲染抽屉菜单,并设置了当点击后替换右边的内容视图的点击事件。
完整代码:
package com.example.lt.toolbar;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.vie