1.打开Project structure,选择app modules,切换到Dependencies添加com.android.support.design.26.0.0.alpha1
2.在layout中添加toolbar

<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.homelink.testtoolbar4.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="50dp" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <!--内容--> </android.support.constraint.ConstraintLayout>
3.去掉默认的ActionBar。
在values下styles.xml中添加样式:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<!-- activity theme. 无ActionBar样式:-->
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
弹出菜单的样式:
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
打开manifests下的AndroidManifest.xml加入android:theme="@style/AppTheme.NoActionBar"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.homelink.testtoolbar4">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
4.在Activity中添加Toolbar的设置:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// 设置图标
toolbar.setLogo(R.mipmap.ic_launcher);
// 设置标题
toolbar.setTitle("Title");
// 设置副标题
toolbar.setSubtitle("Sub title");
// 设置返回按钮图标
toolbar.setNavigationIcon(R.drawable.hwpush_ic_toolbar_back);
setSupportActionBar(toolbar);
// 添加默认返回按钮
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// 返回按钮的点击事件
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "点击了左侧导航按钮", Toast.LENGTH_SHORT).show();
}
});
}
然后默认的ActionBar没有了
5.加入右边菜单项
添加menu,menu_default.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_edit"
android:title="create"
android:orderInCategory="1"
android:icon="@android:drawable/ic_menu_edit"
app:showAsAction="collapseActionView" />
<item
android:id="@+id/action_more"
android:title="more"
android:orderInCategory="2"
android:icon="@android:drawable/ic_menu_more"
app:showAsAction="ifRoom" />
</menu>
5.在MainActivity.java中重写两个方法,实现菜单及其按钮的点击事件
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_default, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_edit:
Toast.makeText(getBaseContext(), "点击了edit图标", Toast.LENGTH_LONG).show();
break;
case R.id.action_more:
Toast.makeText(getBaseContext(), "点击了more图标", Toast.LENGTH_LONG).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}