为什么 Android 中 Toolbar.setTitle() 没有效果

来源:互联网 发布:多少人得空调病 数据 编辑:程序博客网 时间:2024/05/23 11:49
 

为什么 Android 中 Toolbar.setTitle() 没有效果

标签: android
 2490人阅读 评论(0) 收藏 举报
 分类:

目录(?)[+]

问题

现在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-shown
  • http://www.4answered.com/questions/view/20899c6/getSupportActionBarsetTitle-vs-toolbarsetTitle
原创粉丝点击