效果:
首先工程需要引用 v7的
修改values/style.xml
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light"></style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<!-- 修改style -->
<style name="AppTheme.Base" parent="Theme.AppCompat">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<!-- Actionbar color -->
<item name="colorPrimary">@color/link_text_material_light</item>
<!-- Status bar color -->
<item name="colorPrimaryDark">@color/link_text_material_light</item>
<!-- Window color -->
<item name="android:windowBackground">@color/dim_foreground_material_dark</item>
<!-- 將新加入的風格 AppTheme.MyDrawerStyle 設定給 drawerArrowStyle 這個屬性 -->
<item name="drawerArrowStyle">@style/AppTheme.MyDrawerArrowStyle</item>
</style>
<!-- 加入一個新的 navigation drarwer 的風格 -->
<style name="AppTheme.MyDrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<!-- 將 spinBars 屬性設定為 false -->
<item name="spinBars">false</item>
<!-- 設定 drawer arrow 的顏色 -->
<item name="color">@android:color/black</item>
</style>
</resources>
现在布局中可以加入ToolBar了
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize" >
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
android:fitsSystemWindows="true" >
<!-- Content -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="主界面"
android:textColor="#000"
android:textSize="30sp" />
</RelativeLayout>
<!-- Side Drawer -->
<LinearLayout
android:id="@+id/drawer_view"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@drawable/a"
android:fitsSystemWindows="true"
android:orientation="vertical" >
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 设置ToolBar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// Title
toolbar.setTitle("Toolbar的标题");
// App Logo
// toolbar.setLogo(R.drawable.ic_launcher);
// Sub Title
// toolbar.setSubtitle("Sub title");
if (toolbar != null) {
setSupportActionBar(toolbar);
}
// Navigation Icon 要設定在 setSupoortActionBar 才有作用
// 否則會出現 back button
toolbar.setNavigationIcon(R.drawable.perm_group_system_tools);
// 打開 up button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
// 實作 drawer toggle 並放入 toolbar
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open,
R.string.drawer_close);
mDrawerToggle.syncState();
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
github :https://github.com/18236887539/ToolBar
欢迎star