为什么 Android 中 Toolbar.setTitle() 没有效果
2017-06-05 11:44
651 查看
为什么 Android 中 Toolbar.setTitle() 没有效果
标签: android2016-08-16 15:28 2490人阅读 评论(0) 收藏 举报
分类:
Android(17)
版权声明:本文为博主原创文章,未经博主允许不得转载。
目录(?)[+]
问题
现在App中基本都会用Toolbar来代替ActionBar,下面是可能的代码片段。布局文件
<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"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:titleTextColor="@android:color/white"/> </LinearLayout>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代码
public class MainActivity extends ActionBarActivity { Toolbar mActionBarToolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar); mActionBarToolbar.setTitle("我的标题"); setSupportActionBar(mActionBarToolbar); }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 代码中的最后三行改成如下所示的代码即可:mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar); setSupportActionBar(mActionBarToolbar); getSupportActionBar().setTitle("我的标题");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.
mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar); mActionBarToolbar.setTitle(""); setSupportActionBar(mActionBarToolbar);1
2
3
1
2
3
later…
mActionBarToolbar.setTitle(title);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 布局文件如下所示:<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/confirm_order_mail_layout" android:layout_width="match_parent" android:layout_height="fill_parent"> <android.support.design.widget.AppBarLayout android:id="@+id/confirm_order_appbar_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/confirm_order_list_collapsing_toolbar" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary" app:expandedTitleMarginEnd="64dp" app:expandedTitleMarginStart="48dp" app:layout_scrollFlags="scroll|enterAlways"> <android.support.v7.widget.Toolbar android:id="@+id/confirm_order_toolbar_layout" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:layout_scrollFlags="scroll|enterAlways" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> </android.support.v7.widget.Toolbar> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> </android.support.design.widget.CoordinatorLayout>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() 方法:
CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.confirm_order_mail_layout); collapsingToolbarLayout.setTitle("My Title");1
2
1
2
* 这种情况没有使用过,来自下面的参考资料 *
参考资料
http://stackoverflow.com/questions/26486730/in-android-app-toolbar-settitle-method-has-no-effect-application-name-is-shownhttp://www.4answered.com/questions/view/20899c6/getSupportActionBarsetTitle-vs-toolbarsetTitle
相关文章推荐
- 为什么 Android 中 Toolbar.setTitle() 没有效果
- 为什么android逐帧动画没有效果
- Android关于setExitTransition() 没有效果的问题
- android GridView横向滑动后 为什么MyGridView.setSelection(position) 就没效果了
- 转载:为什么首次进入界面是SwipeRefreshLayout.setRefreshing(true)没有动画效果
- 为什么Android没有iOS那么顺滑
- ListView setOnItemClickListener没有效果的解决
- 为什么Android没有iOS那么顺滑
- 为什么Android没有iOS顺滑zz
- setInterval的使用练习——实现网页标题title的滚动效果。
- 用Android中的ActionBar来实现分页Title栏滑块效果
- 为什么Android没有iOS那么顺滑
- 为什么没有好用的Android游戏引擎?
- 为什么Android没有iOS那么顺滑
- android 分页Title栏滑块效果--ActionBar(模拟网易 腾讯等动态效果)
- 用Android中的ActionBar来实现分页Title栏滑块效果
- 为什么Android没有iOS那么顺滑
- 为什么Android没有iOS那么顺滑
- 为什么没有好用的Android游戏引擎?
- 技术控解释为什么Android没有iOS那么顺滑