首先说下配置工做,由于如今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 <resources> <!-- Base application theme. --> <style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar"> <item name="windowActionBar">false</item> <item name="android:windowNoTitle">true</item> <!-- Actionbar color 此处是ToolBar背景色--> <item name="colorPrimary">@color/accent_material_dark</item> <!--Status bar color 此处是状态栏颜色--> <item name="colorPrimaryDark">@color/accent_material_light</item> <!--Window color--> <item name="android:windowBackground">@color/dim_foreground_material_dark</item> </style> </resources>
接下来是Layout文件:java
<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" tools:context=".MainActivity"> <!--Toolbar的背景使用Style里的定义--> <android.support.v7.widget.Toolbar 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.support.v4.widget.DrawerLayout android:id="@+id/drawer" android:layout_below="@+id/toolbar" android:layout_width="match_parent" android:layout_height="match_parent" > <!--DrawerLayout内部的布局份有且只能两部分,主内容区域在嘴上,侧滑栏区域在下面--> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="背景部分" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout> <!--ListView必须设置layout_gravity,代表侧滑栏滑出的方向--> <ListView android:id="@+id/listDrawer" android:layout_gravity="right" android:layout_width="100dp" android:layout_height="match_parent"/> </android.support.v4.widget.DrawerLayout> </RelativeLayout> 突然想到了,attr这个属性是针对styles文件进行解析的,匹配对应名字的颜色
最后是Activity:android
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<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); adapter = new ArrayAdapter<String>(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的布局文件:app
<menu 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" tools:context=".MainActivity"> <!--设置toolbar右侧setting部分的图标内容,showAsAction设置成always图片才能被本身的成功替换掉--> <item android:id="@+id/action_settings" android:title="@string/action_settings" android:icon="@drawable/common_btn_submit_nor" android:orderInCategory="100" app:showAsAction="always" /> </menu>
