一、Toolbar的简单使用
1.因为Toolbar的出现是来替代原先的ActionBar的,因此在使用它之前应该将ActionBar隐藏掉。如果我们没有更改过主题的设置,那么在AndroidManifest.xml文件中的application的theme属性值应该为AppTheme:
<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">
然后打开values/style.xml,将该主题的父主题设置成不带ActionBar的即可:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
2.在布局文件中引入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=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
这里我只在约束布局中放入了一个Toolbar控件,并为它设置了一些属性。宽度设置为填充父容器,高度设置为actionBar的高度,背景色设置为colorPrimary,然后将toolbar的theme属性设置为深色主题(Dark)从而使Toolbar上的文本字样为浅色(白色),将popupTheme设置为浅色主题(Light)从而使Toolbar打开的菜单栏为浅色(白色)。
3.在Activity中使用Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
二、更改Toolbar上显示的文本
更改AndroidManifest.xml中对应活动的label属性值,从而修改标题栏上显示的文字内容
<activity android:name=".MainActivity"
android:label="Fruits">
三、为Toolbar添加Menu
1.创建菜单资源文件
在res目录下新建一个menu文件夹,然后新建一个toolbar.xml的资源文件
<?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/backup"
android:icon="@drawable/backup"
android:title="Backup"
app:showAsAction="always"/>
<item
android:id="@+id/delete"
android:icon="@drawable/delete"
android:title="Delete"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/settings"
android:icon="@drawable/settings"
android:title="Settings"
app:showAsAction="never"/>
</menu>
这里我们通过item标签来设置Toolbar上的action按钮,id属性作为唯一标识,icon属性用于指定按钮图标,title属性用于指定按钮的文字,showAsAction属性用于指定按钮的位置:always表示永远显示在Toolbar中,如果屏幕空间不够则不显示;ifRoom表示屏幕空间足够的情况下显示在Toolbar中,不够的话就显示在菜单中;never表示永远显示在菜单中。
PS:Toolbar中的action按钮只会显示图标,菜单中的action按钮只会显示文字。
2.在Activity中重写onCreateOptionsMenu()方法
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar,menu);
return true;
}
PS:返回true表示允许创建菜单。
3.在Activity中重写onOptionsItemSelected()方法
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.backup:
Toast.makeText(this,"You clicked Backup",Toast.LENGTH_SHORT).show();
break;
case R.id.delete:
Toast.makeText(this,"You clicked Delete",Toast.LENGTH_SHORT).show();
break;
case R.id.settings:
Toast.makeText(this,"You clicked Settings",Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}
四、为Toolbar启用返回按钮
说是返回按钮,其实是左上角的一个左箭头,Android为其命名为HomeAsUp
1.在Activity中获取ActionBar对象,并启用该按钮
ActionBar actionBar = getSupportActionBar();
if(actionBar!=null)
actionBar.setDisplayHomeAsUpEnabled(true);
2.在onOptionsItemSelected()方法中设置该按钮的点击事件
case android.R.id.home:
finish();
break;