Toolbar的setTitle问题

陈一苇 2016-03-18 11:14:10

在setSupportActionBar(toolbar)之后调用toolbar.setTitle()的话。
在onCreate()中调用无效。
在onStart()中调用无效。
在onResume()中调用有效。
请问这个是什么原因,研究了好久实在没找到。求大神详解。好人一生平安。
...全文
308 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
magina920 2016-06-30
  • 打赏
  • 举报
回复
getSupportActionBar.setTitle()
ganshenml 2016-03-18
  • 打赏
  • 举报
回复
从stackOverFlow的答案中: 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. mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar); mActionBarToolbar.setTitle(""); setSupportActionBar(mActionBarToolbar); later... mActionBarToolbar.setTitle(title); 地址:http://stackoverflow.com/questions/26486730/in-android-app-toolbar-settitle-method-has-no-effect-application-name-is-shown 写在onStart里面,当程序挂起的时候再次进入,应该是能看到title的。 但是第一次是没有看到,或许可以推测是否有库对flag进行判断第一次进入的时候就不进行某些操作了,因为第二次进入的时候已经有设置了的title了,所以就直接进行了调用。 (仅为推测,具体不知库是怎么实现的)
陈一苇 2016-03-18
  • 打赏
  • 举报
回复
引用 2 楼 ganshenml 的回复:
代码贴出来
代码没有什么特殊啊,就是在onCreate()方法中使用toolbar的时候发现在setSupportActionBar()之后调用toolbar.setTitle()没有效果,于是在onStart()和onResume()方法中都试了一下修改title,发现正在onResume()中的修改是有效果的。但是跟了源码没找到原因。
ganshenml 2016-03-18
  • 打赏
  • 举报
回复
代码贴出来
tcmakebest 2016-03-18
  • 打赏
  • 举报
回复
放到onResume就好了毕竟这个方法是必然会运行到的,想知道原因就下载源代码去研究吧

80,490

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧