首先说下配置工作,因为现在Android机所采用的版本4.0以下都比较少了,所以我司的APP基本是把最低版本定在了4.0(3.0以Pad为主嘛,至于android的Pad,算了,不提也罢),否则会报“java.lang.IllegalArgumentException: AppCompat does not support the current theme features”的错误,[stackOverFlow上有相关错误](http://http://stackoverflow.com/questions/29784124/java-lang-illegalargumentexception-appcompat-does-not-support-the-current-theme)。下面上代码,首先是Style文件:
//此处主题必须设置为Theme.AppCompat.NoActionBar,即隐藏ActionBar
false
true
@color/accent_material_dark
@color/accent_material_light
@color/dim_foreground_material_dark
接下来是Layout文件:
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity">
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:layout_alignParentTop="true"
/>
android:id="@+id/drawer"
android:layout_below="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
android:layout_width="match_parent"
android:layout_height="match_parent">
android:text="背景部分"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:id="@+id/listDrawer"
android:layout_gravity="right"
android:layout_width="100dp"
android:layout_height="match_parent"/>
忽然想到了,attr这个属性是针对styles文件进行解析的,匹配对应名字的颜色
最后是Activity:
package waiqin.example.com.drawerlayoutapp;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private DrawerLayout drawer;
private ListView list;
private String [] array = {"条目1","条目2","条目3"};
private ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,array);
initToolbar();
initView();
}
private void initToolbar()
{
toolbar = (Toolbar) findViewById(R.id.toolbar);
//设置toolbar标题
toolbar.setTitle("Drawer");
//设置Navigation
toolbar.setNavigationIcon(R.drawable.common_btn_back);
//设置点击Navigation的方法
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
//为了让ToolBar各项设置都生效的话,这个方法必须放到最后
setSupportActionBar(toolbar);
}
private void initView()
{
drawer = (DrawerLayout) findViewById(R.id.drawer);
list = (ListView) findViewById(R.id.listDrawer);
list.setAdapter(adapter);
}
//此方法定义Menu的布局样式,返回false则不显示Menu
@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;
}
//此方法定义点击Menu按钮产生的事件
@Override
public boolean onOptionsItemSelected(MenuItem 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) {
if (drawer.isDrawerOpen(Gravity.RIGHT))
{
drawer.closeDrawer(Gravity.RIGHT);
}
else
{
drawer.openDrawer(Gravity.RIGHT);
}
return true;
}
return super.onOptionsItemSelected(item);
}
}
最后是Menu的布局文件:
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
android:icon="@drawable/common_btn_submit_nor"
android:orderInCategory="100" app:showAsAction="always" />
![效果如图:](https://static.oschina.net/uploads/img/201508/23161841_jjG2.jpg "在这里输入图片标题")