package com.zjw.mymaterialdesign2; import android.os.Bundle; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; import butterknife.BindView; import butterknife.ButterKnife; //12.3 p415 滑动菜单 //12.3.1 p415 DrawerLayout /* 用法: 该布局允许放入两个直接子控件, 第一个:主屏幕中显示的内容 第二个:滑动菜单显示内容 <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.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:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.zjw.mymaterialdesign2.UseOfDrawerLayout"> <!--主屏幕中显示的内容--> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v7.widget.Toolbar android:id="@+id/tb" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> </android.support.v7.widget.Toolbar> </FrameLayout> <!--滑动菜单显示内容--> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="this is menu" android:textSize="30sp" android:background="#fff" android:layout_gravity="start"/> </android.support.v4.widget.DrawerLayout> 其中android:gravity="start"属性必须指定,即指定滑动菜单DrawerLayout在屏幕左边还是右边 left:左,right:右 start:会根据系统语言进行判断,系统语言从左往右就在左,从右往左(如阿拉伯语)就在右 */ /* 提示用户有侧滑菜单:Toolbar加导航按钮 再扔一张导航按钮图标进文件夹 调用 actionBar.setDisplayHomeAsUpEnabled(true);//让导航按钮显示出来 actionBar.setHomeAsUpIndicator(R.mipmap.left);//设置导航按钮图标 默认图标是一个返回箭头 设置点击事件 case android.R.id.home://导航按钮HomeAsUp的id mDrawerLayout.openDrawer(GravityCompat.START); */ public class UseOfDrawerLayout extends AppCompatActivity { @BindView(R.id.tb) Toolbar mTb; @BindView(R.id.drawer_layout) DrawerLayout mDrawerLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_use_of_drawer_layout); ButterKnife.bind(this); setSupportActionBar(mTb); ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { actionBar.setDisplayHomeAsUpEnabled(true);//让导航按钮显示出来 actionBar.setHomeAsUpIndicator(R.mipmap.left);//设置导航按钮图标 } } //加载Menu布局 @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.toolbar, menu); return true; } //按钮点击事件 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.tb_item_backup: showToast("you clicked backup"); break; case R.id.tb_item_delete: showToast("you clicked delete"); break; case R.id.tb_item_setting: showToast("you clicked setting"); break; case android.R.id.home://导航按钮HomeAsUp的id mDrawerLayout.openDrawer(GravityCompat.START); break; default: break; } return true; } //专门弹吐司 private Toast mToast; public void showToast(String msg) { if (mToast == null) { mToast = Toast.makeText(this, "", Toast.LENGTH_SHORT); } mToast.setText(msg); mToast.show(); } }
转载于:https://my.oschina.net/u/3620480/blog/1494684