- 使用系统自带的
1, Style
2, <!--Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimary">@color/accent_material_dark</item>
<item name="colorPrimaryDark">@color/accent_material_light</item>
<item name="android:windowBackground">@color/dim_foreground_material_dark</item>
</style>
3, Xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--<include layout="@layout/activity_toolbar_title"/>-->
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary">
</android.support.v7.widget.Toolbar>
</LinearLayout>
4, java代码
/**
*设置toolbar
*/
private void setToolbar(){
Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);
//设置APP图标
toolbar.setLogo(R.mipmap.ic_launcher);
//设置title
toolbar.setTitle("欢迎页");
//设置副标题 zxcv
toolbar.setSubtitle("副标题");
setSupportActionBar(toolbar);
//设置toolabar
//设置导航图标一定要设置在setsupportactionbar后面才有用不然他会显示小箭头
toolbar.setNavigationIcon(R.mipmap.ic_launcher);
toolbar.setOnMenuItemClickListener(onMenuItemClick);
}
private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
String msg = "";
switch (menuItem.getItemId()) {
//进行设置toolbar中menuitem的点击事件
}
if(!msg.equals("")) {
}
return true;
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.welcome_menu_item,menu);
return true;
}
2.自定义
自定义toolbar可以在
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary">
</android.support.v7.widget.Toolbar>
这个里面添加子控件