Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
That is the navigation bar at the top of the Toolbar, use is also very simple. Here are the steps.
- Defining a style ActionBar style.xml removed in the.
<!--关掉ActionBar-->
<style name="AppCompatTheme" parent="Theme.AppCompat.Light.NoActionBar"/>
- Modifying activity in AndroidManifest.xml node android: theme property.
<activity android:name=".ToolbarDemoActivity" android:theme="@style/AppCompatTheme"/>
- Use ToolBar in the layout file.
<androidx.appcompat.widget.Toolbar
android:id="@+id/tb_head"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
- Tags and comments provided ToolBar common properties as follows, xml file similar settings.
toolbar_head = findViewById(R.id.tb_head);
//设置标题文字
toolbar_head.setTitle("测试页面");
//社招左侧导航图标
toolbar_head.setNavigationIcon(R.drawable.ic_back);
//设置标题文字颜色
toolbar_head.setTitleTextColor(Color.YELLOW);
//设置工具栏图标
toolbar_head.setLogo(R.drawable.head_demo);
//设置副标题文字
toolbar_head.setSubtitle("测试用的Toolbar");
//设置副标题文字颜色
toolbar_head.setSubtitleTextColor(Color.GRAY);
//设置背景颜色
oolbar_head.setBackgroundResource(R.color.blue_light);
//给当前页面设置ToolBar
this.setSupportActionBar(toolbar_head);
//设置监听 必须放在setActionBar之后
toolbar_head.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//do something
}
});