使用条件,需要添加support v7包
dependencies {
compile ‘com.android.support:appcompat-v7:21.+’ //具体根据你SDK中support包的版本而定
}
布局文件部分
layout/tool_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar">
</android.support.v7.widget.Toolbar>
layout/drawerlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" //这个layout必须放在最外层,否则会造成拉出菜单,其他view会在它之上
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/dl_left"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--主布局--> 将你要使用的View放在这个layout里,因为它是主界面
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.hzwangwentong.ripplelayout.MaterialRippleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffff00"
app:rippleOverlay="true"
app:rippleColor="#03A9F4"
app:rippleAlpha="0.2"
app:rippleDelayClick="false"
app:rippleHover="true"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_alignParentStart="true">
<Button
android:id="@+id/ripple_layout_1"
android:layout_width="match_parent"
android:background="@drawable/selector"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="通过xml文件设置"/>
</com.example.hzwangwentong.ripplelayout.MaterialRippleLayout>
<Button
android:id="@+id/ripple_layout_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/selector"
android:text="通过代码设置"
android:layout_below="@+id/button"
android:layout_alignParentStart="true" />
</RelativeLayout>
<!--侧滑菜单-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:layout_gravity="start">
<ListView
android:id="@+id/lv_left_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:text="DrawerLayout" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
最后放入布局界面
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".DemoActivity"
android:fitsSystemWindows="true"
>
<!--Toolbar-->
<include layout="@layout/tool_bar"
android:id="@+id/toolbar" />
<!--DrawerLayout-->
<include layout="@layout/drawerlayout"
android:layout_below="@+id/toolbar"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</RelativeLayout>
由于使用Toolbar,所以需要使用NoActionbar主题
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<!--状态栏颜色-->
<item name="colorPrimaryDark">@color/Indigo_colorPrimaryDark</item>
<!--Toolbar颜色-->
<item name="colorPrimary">@color/Indigo_colorPrimary</item>
<!--返回键样式-->
<item name="drawerArrowStyle">@style/AppTheme.DrawerArrowToggle</item>
</style>
<style name="AppTheme.DrawerArrowToggle" parent="Base.Widget.AppCompat.DrawerArrowToggle"> 这是左边菜单栏图标的主题
<item name="color">@android:color/white</item>
</style>
java代码部分
private Toolbar toolbar;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private ListView lvLeftMenu;
private String[] lvs = {"List Item 01", "List Item 02", "List Item 03", "List Item 04"};
private ArrayAdapter arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
toolbar = (Toolbar) findViewById(R.id.toolbar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.dl_left);
lvLeftMenu = (ListView) findViewById(R.id.lv_left_menu);
toolbar.setTitle("Toolbar");//设置Toolbar标题
toolbar.setTitleTextColor(Color.parseColor("#ffffff")); //设置标题颜色
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//创建返回键,并实现打开关/闭监听
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.open, R.string.close) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
};
mDrawerToggle.syncState();
mDrawerLayout.setDrawerListener(mDrawerToggle);
//设置菜单列表
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, lvs);
lvLeftMenu.setAdapter(arrayAdapter);
......
......
}