android.support.v7.widget.Toolbar Android5.0推出,可以向下兼容
因为ActionBar设计不灵活,android L引入的一个新控件Toolbar,用来取代ActionBar
使用ToolBar需要去掉Activity原有标题栏,否则会报错,去掉ActionBar的方式有如下几种:
- 代码调用:requestWindowFeature(Window.FEATURE_NO_TITLE)这种在Activity中使用,
supportRequestWindowFeature(Window.FEATURE_NO_TITLE)这种在AppCompatActivity中使用 - 使用没有ActionBar的主题, 如:Theme.AppCompat.Light.NoActionBar
- 使用有ActionBar的主题,然后通过属性去掉ActionBar
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowNoTitle">true</item>
</style>
- 布局文件属性说明
<android.support.v7.widget.Toolbar
<!--这里代表使用系统默认的ActionBar的高度-->
android:layout_height="?attr/actionBarSize"
<!--这里代表使用系统默认的colorPrimary的颜色,见下图标识区域-->
android:background="@color/colorPrimary"
<!--这里将主题设置为深色,是为了让Toolbar上的图标和字体显示为白色-->
app:theme="@style/ThemeOverlay.AppCompat.Dark"
<!--这里使设置弹出窗的主题为亮色,默认为黑色,右上角点击弹出的窗口主题-->
app:popupTheme="@style/Theme.AppCompat.Light"/>
- 代码中也可以对Toolbar进行属性设置
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar); // 使用toolBar代替ActionBar
toolbar.setNavigationIcon(android.R.drawable.ic_delete); // 设置导航按钮
toolbar.setLogo(R.mipmap.ic_launcher); // 设置logo图片
toolbar.setTitle("ToolBar Title"); // 设置标题
toolbar.setTitleTextColor(Color.RED); // 设置标题颜色
toolbar.setSubtitle("This is subtitle"); // 设置子标题
- ToolBar右上角菜单项显示及点击事件
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.base_toolbar_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_share) {
return true;
}
return super.onOptionsItemSelected(item);
}
- 代码中R.menu.base_ toolbar_menu布局文件在menu文件夹下
<?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/action_search"
android:orderInCategory="100"
android:icon="@drawable/ic_menu_search"
android:title="搜索"
<!--app:showAsAction若为ifRoom则,作为app bar的按钮,
若为never则为overflow menu中的选项,若app bar中的空间不足,
则会将超出的部分全部放入overflow menu中-->
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_share"
android:orderInCategory="100"
android:icon="@android:drawable/ic_menu_share"
android:title="分享"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:icon="@drawable/ic_menu_manage"
android:title="设置"
app:showAsAction="never" />
<item
android:id="@+id/action_camera"
android:orderInCategory="100"
android:icon="@drawable/ic_menu_camera"
android:title="拍照"
app:showAsAction="never" />
</menu>
- 在menu中,图标不显示解决方案
- 有的人是采用 onMenuOpened() 来写,可是仍然会有问题,对于AppCompactActivity你可以把onPrepareOptionsPanel(View v,Menu menu)代替 onMenuOpened()
protected boolean onPrepareOptionsPanel(View view, Menu menu) {
if (menu != null) {
if (menu.getClass() == MenuBuilder.class) {
try {
Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
} catch (Exception e) {
System.out.println(getClass().getSimpleName() + "onMenuOpened...unable to set icons for overflow menu" + e);
}
}
}
return super.onPrepareOptionsPanel(view, menu);
}
- Toolbar代码中修改属性
toolbar.setLogo(R.mipmap.ic_launcher); // 设置logo图片
//toolbar.setTitle("ToolBar Title"); //直接设置标题是没有效果的
getSupportActionBar().setTitle("ToolBar Title");// 设置标题
toolbar.setTitleTextColor(Color.RED); // 设置标题颜色
toolbar.setSubtitle("This is subtitle"); // 设置子标题
- Toolbar中menu点击事件处理
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
switch (id){
case R.id.action_search:
Toast.makeText(this, "搜索", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_share:
Toast.makeText(this, "分享", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_settings:
Toast.makeText(this, "设置", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_camera:
Toast.makeText(this, "拍照", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
更详细参考学习案例转载:http://www.codeceo.com/article/android-toolbar-develop.html