当使用toolbar的时候,你需要引用appcompat-v7的支持,并设置主题为NoActionBar。
然后在代码中获取到toolbar并设置toolbar的一些属性,通过setSupportActionBar()
方法设置toolbar为ActionBar。菜单的配置项与ActionBar基本类似。
在drawlayout中,你需要定义两个界面,一个是主界面,一个是侧滑菜单界面。
在侧滑菜单中你需要指定android:layout_gravity="start|left"设置水平重力。
然后你需要定义ActionBarDrawerToggle,然后通过其方法开始同步,然后设置
drawlayout的监听为drawerToggle。
代码如下:
//app_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent1"></android.support.v7.widget.Toolbar>
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="com.shuaijie.jiang.theme.MainActivity">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
<android.support.v4.widget.DrawerLayout
android:id="@+id/draw"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="@color/colorPrimaryDark1"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"></LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
//menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/ab_search"
android:orderInCategory="80"
android:title="action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_setting"
android:orderInCategory="100"
android:title="action_setting"
app:showAsAction="never" />
</menu>
//MainActivity.java
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private DrawerLayout mDrawer;
private ActionBarDrawerToggle drawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.app_bar);
mDrawer = (DrawerLayout) findViewById(R.id.draw);
toolbar.setLogo(R.mipmap.ic_launcher);
toolbar.setTitle("主标题");
toolbar.setSubtitle("副标题");
setSupportActionBar(toolbar);//将Toolbar设置成ActionBar
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
drawerToggle = new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_des_open, R.string.drawer_des_close);
//<string name="drawer_des_open">open</string>
//<string name="drawer_des_close">close</string>
mDrawer.setDrawerListener(drawerToggle);//设置监听
drawerToggle.syncState();//开启同步
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
}
转载请注明原文地址: https://ju.6miu.com/read-1293800.html