ToolBar的功能主要是在Activity上加上标题栏,用来替代Actionbar。
简单的使用如下:
1首先你必须在工程里修改styles.xml文件里的主题风格,系统默认如下
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
修改为如下设置:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
2.在布局文件里增加ToolBar控件,如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- 以下是ToolBar设置-->
<androidx.appcompat.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:theme="?attr/actionBarTheme"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp"
app:popupTheme="@style/ToolBarPopTheme"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
3.在代码中调用
package com.example.toolbar;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import java.util.Objects;
public class MainActivity extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//以下是ToolBar的调用和设置
setSupportActionBar((androidx.appcompat.widget.Toolbar) findViewById(R.id.toolbar));
getSupportActionBar().setDisplayShowTitleEnabled(false);//隐藏默认的Title
((Toolbar) findViewById(R.id.toolbar)).setTitle("主标题");
((Toolbar)findViewById(R.id.toolbar)).setSubtitle("副标题");
//((Toolbar)findViewById(R.id.toolbar)).setLogo(R.mipmap.ic_launcher);//设置标题LOGO
((Toolbar)findViewById(R.id.toolbar)).setSubtitleTextColor(Color.WHITE);//设置副标题文本颜色
((Toolbar)findViewById(R.id.toolbar)).setNavigationIcon(R.mipmap.ic_launcher);//设置导航图标
((Toolbar)findViewById(R.id.toolbar)).setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int menuItemId=item.getItemId();
if (menuItemId==R.id.action_exit){
Toast.makeText(getApplication(),"你点击了退出按钮",Toast.LENGTH_LONG).show();
}
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu,menu);
return true;
}
}
4.增加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-->
<item
android:id="@+id/action_edit"
android:icon="@mipmap/ic_launcher"
android:orderInCategory="80"
android:title="查找"
app:showAsAction="always"/>
<item
android:id="@+id/action_set"
android:icon="@mipmap/ic_launcher_round"
android:title="设置"
app:showAsAction="always"/>
<item
android:id="@+id/action_exit"
android:icon="@drawable/ic_launcher_foreground"
android:title="退出"
app:showAsAction="never"/>
</menu>
其中app:showAsAction有以下几种选项:
1.always:总是显示在界面上
2.never:不显示在界面上,只让出现在右边的三个点中
3.ifRoom:如果有位置才显示,不然就出现在右边的三个点中
三个点中的菜单样式我们可以自己修改或使用系统默认。如果自己修改,可以设置一个样式,如下:
<style name="ToolBarPopTheme" parent="@style/ThemeOverlay.AppCompat">
<item name="android:colorBackground">#8BC34A</item>
<item name="android:textColor">#64FFDA</item>
</style>
我这里只是修改了背景色和文本颜色。设置好后,在ToolBar中加下面一行代码:
app:popupTheme="@style/ToolBarPopTheme"
点击响应的代码包含在前面的Activity代码中。
以上只是ToolBar的一些简单用法例程,做为学习整理方便以后查找。