为什么 Android 中 Toolbar.setTitle() 没有效果
来源:互联网 发布:多少人得空调病 数据 编辑:程序博客网 时间:2024/05/23 11:49
为什么 Android 中 Toolbar.setTitle() 没有效果
版权声明:本文为博主原创文章,未经博主允许不得转载。
目录(?)[+]
问题
现在App中基本都会用Toolbar来代替ActionBar,下面是可能的代码片段。
布局文件
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
Java代码
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
但是这样该 Activity 的标题并不是预想中的“我的标题”,而是 App 应用的名字,即 AndroidManifest.xml 文件中 application 元素的 label 属性值。
这是什么原因,又怎样解决呢?
办法
其实解决办法很简单,只需要将 Java 代码中的最后三行改成如下所示的代码即可:
- 1
- 2
- 3
- 1
- 2
- 3
除了这种方案外,还有一种解决办法在下面。
原因
产生这种现象的原因通过 Google 查了一下,资料不是很多,但还是可以了解一些的,下面是几种解释(内容很简单,就没有翻译)。
- 解释一:
The internal implementation of the support library just checks if the Toolbar has a title (not null) at the moment the SupportActionBar is set up. If there is, then this title will be used instead of the window title. You can then set a dummy title while you load the real title.
- 1
- 2
- 3
- 1
- 2
- 3
later…
- 1
- 1
- 解释二:
If you call setSupportActionBar(Toolbar), then the Action Bar is then responsible for handling the title, therefore you need to call getSupportActionBar().setTitle(“My Title”); to set a custom title.
We can have multiple toolbars as layout widget but action is not. Thus better approach is to use getSupportActionBar().setTitle(“My Title”);
另一种情况
如果你想结合 CollapsingToolbarLayout 和 Toolbar 一起使用,那么上面的解决方式就不适用了,假设我们的 XML 布局文件如下所示:
- 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
- 34
- 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
- 34
这时我们需要调用 CollapsingToolbarLayout 的 setTitle() 方法:
- 1
- 2
- 1
- 2
* 这种情况没有使用过,来自下面的参考资料 *
参考资料
- http://stackoverflow.com/questions/26486730/in-android-app-toolbar-settitle-method-has-no-effect-application-name-is-shown
- http://www.4answered.com/questions/view/20899c6/getSupportActionBarsetTitle-vs-toolbarsetTitle
- 为什么 Android 中 Toolbar.setTitle() 没有效果
- 为什么 Android 中 Toolbar.setTitle() 没有效果
- Toolbar setTitle()
- ToolBar setTitle不起作用
- toolbar setTitle 无效的解决方法
- 为什么android逐帧动画没有效果
- ToolBar 使用 toolbar setTitle不显示
- ToolBar的setTitle()方法不生效解决方法
- CollapsingToolbarLayout时Toolbar.setTitle不生效
- Android中关于setTitle(CharSequence title)的知识总结
- Android 中CollapsingToolbarLayout和Toolbar实现炫酷效果
- Android中DrawerLayout+Toolbar实现侧滑效果[DrawerLayout+ToolBar+NavigationView]
- ToolBar和ToolBar的setTitle()方法不生效解决方法
- Android中Toolbar (1)
- Android中Toolbar (2)
- Android toolbar阴影悬浮高亮效果
- Android Support Library-标题栏(Toolbar)滚动效果
- Android DrawerLayout+Toolbar+NavigationView(实现侧拉侧滑效果)
- Android getContext().getSystemService()
- FTL常用标签及语法
- linux su和sudo命令的区别
- mysq用户自定义完整性之 添加检查约束。
- 你想要的CacheUtils
- 为什么 Android 中 Toolbar.setTitle() 没有效果
- LeetCode 22 Generate Parentheses
- Vladik and Courtesy
- linux crontab 重复执行
- Android 分享
- 安卓实用工具
- jQuery中宽、高、位置和滚动条总结
- lcd参数解释及计算
- APK空包签名