摘自:安卓APP_ 控件(7)——Toolbar栏目样式
作者:丶PURSUING
发布时间: 2021-04-02 15:42:07
网址:https://blog.csdn.net/weixin_44742824/article/details/115395997

Toolbar栏目样式

  • 什么是Toolbar?
  • 设置细节见具体代码
  • ToolBar当然也可以在.java中进行设置
  • 控件的嵌套运用:TextView放在Toolbar中
  • ToolBar的导包问题

什么是Toolbar?


改为NoActionBar,创建属于自己的栏目样式

如下图,自定义了Toolbar样式和其中的navigationIcon返回设置

设置细节见具体代码

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><!--    ?attr/actionBarSize     设置高度为系统的actionBar高度logo                    很少设置navigationIcon          这个图标一般多设置为返回箭头subtitle                子标题一般少设置,出现在主标题的下方subtitleTextColor       子标题文本颜色title                   当前页的主标题titleMarginStart        主标题距离左边的间距titleTextColor          主标题文本颜色设置前缀为app表示用的是androidx里面的属性而不是安卓自带的   --><androidx.appcompat.widget.Toolbarandroid:id="@+id/tool_return"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="@color/teal_200"app:logo="@drawable/ic_baseline_loyalty_24"app:navigationIcon="@drawable/ic_baseline_keyboard_return_24"app:subtitle="    监控中"app:subtitleTextColor="#ff0000"app:title="智能家居"app:titleMarginStart="90dp"app:titleTextColor="@color/purple_700" /></LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

MainActivity.java

public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Toolbar toolbar = findViewById(R.id.tool_return);//这个返回的图标等待被点击,点击后执行的操作。toolbar.setNavigationOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Log.e("zhua", "onClick: toolbar被点击了");}});}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

ToolBar当然也可以在.java中进行设置

例如:

MainActivity.java

Toolbar toolbar2 = findViewById(R.id.tool_return_2);
toolbar2.setNavigationIcon(R.drawable.ic_baseline_keyboard_return_24);
setTitle("标题");
toolbar2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Log.e("zhua", "onClick: toolbar2被点击了");}
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

控件的嵌套运用:TextView放在Toolbar中

如何把主标题放在正中间呢?老知识新用法罢了,控件的组合:TextView放在Toolbar中

<androidx.appcompat.widget.Toolbarandroid:id="@+id/tool_return_2"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:layout_marginTop="20dp"android:background="#ffff00"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:gravity="center"android:text="TextView标题"android:textSize="22dp"android:textColor="#ff0000"android:textStyle="bold" /></androidx.appcompat.widget.Toolbar>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

效果如下图:


ToolBar的导包问题

可能会遇到的ToolBar的导包问题:


要把下面这个修改
导入正确的包

安卓APP_ 控件(7)——Toolbar栏目样式相关推荐

  1. 安卓APP_ 控件(11)webView —— 简单应用:显示网页

    摘自:安卓APP_ 控件(11)webView -- 简单应用:显示网页 作者:丶PURSUING 发布时间: 2021-05-11 11:50:52 网址:https://blog.csdn.net ...

  2. 安卓APP_ 控件(10)—— ListView可上下滑动的列表(重要)与ViewHolder优化

    摘自:安卓APP_ 控件(10)-- ListView可上下滑动的列表(重要)与ViewHolder优化 作者:丶PURSUING 发布时间: 2021-04-12 23:28:27 网址:https ...

  3. 安卓APP_ 控件(9)—— PopupWindow弹窗

    摘自:安卓APP_ 控件(9)-- PopupWindow弹窗 作者:丶PURSUING 发布时间: 2021-04-05 14:41:35 网址:https://blog.csdn.net/weix ...

  4. 安卓APP_ 控件(8)—— AlertDialog

    摘自:安卓APP_ 控件(8)-- AlertDialog 作者:丶PURSUING 发布时间: 2021-04-02 18:13:20 网址:https://blog.csdn.net/weixin ...

  5. 安卓APP_ 控件(6)—— Notification通知

    摘自:安卓APP_ 控件(6)-- Notification通知 作者:丶PURSUING 发布时间: 2021-04-02 00:30:14 网址:https://blog.csdn.net/wei ...

  6. 安卓APP_ 控件(5)—— ProgressBar

    摘自:安卓APP_ 控件(5)-- ProgressBar 作者:丶PURSUING 发布时间: 2021-03-31 13:03:07 网址:https://blog.csdn.net/weixin ...

  7. 安卓APP_ 控件(4)—— ImageView

    摘自:安卓APP_ 控件(4)-- ImageView 作者:丶PURSUING 发布时间: 2021-03-29 21:52:06 网址:https://blog.csdn.net/weixin_4 ...

  8. 安卓APP_ 控件(3)—— EditText

    摘自:安卓APP_ 控件(3)-- EditText 作者:丶PURSUING 发布时间: 2021-03-29 18:43:40 网址:https://blog.csdn.net/weixin_44 ...

  9. 安卓APP_ 控件(2)—— Button

    摘自:安卓APP_ 控件(2)-- Button 作者:丶PURSUING 发布时间: 2021-03-29 14:20:54 网址:https://blog.csdn.net/weixin_4474 ...

最新文章

  1. java中properties作用,java中Properties类的使用
  2. java·环境变量、基本数据类型
  3. 年度总结——文字留住岁月,情感点缀年华
  4. NYOJ 303 序号转换 数学题
  5. PandasSQL语法归纳总结,真的太全了
  6. c++ 二维数组_二维数组的声明2019_04_18
  7. GBDT的回归、二分类以及多分类教程
  8. 辨异 —— 逻辑之辨、人文社科观念
  9. [转载] 机器学习篇—Numpy数值计算基础(中)
  10. MOSS自带链接样式影响页面全局样式的解决办法
  11. 2019刚开年全球天气已“爆表” 多国极端天气打破历史纪录
  12. skyline 系列 3 -TerraBuilder的使用 、mpt的创建和发布
  13. 重整国家资产负债表的核心是谁来买单
  14. 华为手机usb调试打开后自动关闭怎么办?华为手机 usb调试为什么自动关闭?usb调试老是自动关闭怎么回事?...
  15. 名词用作动词举例_英语中名词做动词用的55个例句
  16. 双频段AP wps同时开启并通过智能手机、windows端连接问题
  17. ZZULIOJ:1016: 银行利率
  18. 泛函分析笔记(二十一) 障碍问题
  19. php redis hset过期时间,详解Redis中数据过期策略
  20. 美国国防部发布网络新战略,重点关注中国俄罗斯

热门文章

  1. Oracle ASM 翻译系列第十一弹:高级知识 Offline or drop?
  2. 某公司数据恢复报告书
  3. ffmpeg的新东东:AVFilter
  4. linux下ORACLE的Sqlplus命令
  5. 动态链接库 仅有.dll文件时候的使用方法
  6. Cisco ××× 完全配置指南-连载-SSL ×××
  7. 各类防火墙应用对比分析
  8. 时钟切换处理(Verilog)
  9. Match图像匹配halcon算子,持续更新
  10. HALCON示例程序find_pads.hdev通过fit_rectangle2_contour_xld绘制精准轮廓