ToolBar的使用可参考官网:
https://developer.android.com/training/appbar/setting-up.html#add-toolbar
ActionBarActivity已经被弃用,推荐使用AppCompatActivity
md的主题有:
与之对应的Compat Theme:
Compat Theme能够兼容5.0以下版本适用md主题
要使用md主题,Activity必须继承AppCompatActivity
添加依赖compile ‘com.android.support:appcompat-v7:23.2.1’
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary
- "colorPrimaryDark"
>@color/colorPrimaryDark
- "colorAccent">@color/colorAccent
style>
修改为NoActionBar
这里要注意colorPrimary等属性,对应控制如下图:
这些属性能定制状态栏颜色等
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
/>
由于colorPrimary设置ActionBar的颜色,并不能设置ToolBar的颜色
所以这里手动设置background为@color/colorPrimary
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_main);
toolbar.setTitle("标题");
setSupportActionBar(toolbar);
}
setSupportActionBar() 方法设置toolbar为此Activity的App Bar;
当ToolBar被设置为App Bar,可通过getSupportActionBar()方法获取到一个的ActionBar对象,通过该对象引用可对ToolBar做更多操作;
更多方法请参考API
效果图:
<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="edit"
android:orderInCategory="1"
android:icon="@mipmap/edit_main"
app:showAsAction="ifRoom" />
<item android:id="@+id/action_share"
android:title="share"
android:orderInCategory="2"
android:icon="@mipmap/share_main"
app:showAsAction="ifRoom" />
<item android:id="@+id/action_settings"
android:title="settings"
android:orderInCategory="3"
app:showAsAction="never"/>
menu>
app:showAsAction属性设置是否在App Bar上显示一个按钮
app:showAsAction=”ifRoom” //显示在Bar上
app:showAsAction=”never” //不显示Bar上,在溢出菜单中显示
由于showAsAction属性是在支持包中定义的,所以需要添加命名空间:
xmlns:app="http://schemas.android.com/apk/res-auto"
方式一:
重写onCreateOptionsMenu(Menu menu)、onOptionsItemSelected(MenuItem item)
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_edit:
Toast.makeText(MainActivity.this, "action_s", Toast.LENGTH_LONG).show();
break;
case R.id.action_share:
Toast.makeText(MainActivity.this, "action_share", Toast.LENGTH_LONG).show();
break;
default:
break;
}
return true;
}
方式二:
重写onCreateOptionsMenu(Menu menu),通过toolbar.setOnMenuItemClickListener()监听
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_edit:
Toast.makeText(MainActivity.this, "action_settings", Toast.LENGTH_LONG).show();
break;
case R.id.action_share:
Toast.makeText(MainActivity.this, "action_share", Toast.LENGTH_LONG).show();
break;
default:
break;
}
return true;
}
});
注意:需要將toolbar.setOnMenuItemClickListener()设定在 setSupportActionBar 之后才有作用(setNavigationIcon也需要放在 setSupportActionBar之后才会生效)
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_main);
toolbar.setTitle("标题");
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setHomeButtonEnabled(true) //设置返回键可用
setDisplayHomeAsUpEnabled(true) //设置返回键显示
重写onOptionsItemSelected()监听事件:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
default:
break;
}
return true;
}