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

    xiaoxiao2026-05-14  10

    问题

    现在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>

    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); }

    但是这样该 Activity 的标题并不是预想中的“我的标题”,而是 App 应用的名字,即 AndroidManifest.xml 文件中 application 元素的 label 属性值。

    这是什么原因,又怎样解决呢?

    办法

    其实解决办法很简单,只需要将 Java 代码中的最后三行改成如下所示的代码即可:

    mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar); setSupportActionBar(mActionBarToolbar); getSupportActionBar().setTitle("我的标题");

    除了这种方案外,还有一种解决办法在下面。

    原因

    产生这种现象的原因通过 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);

    later…

    mActionBarToolbar.setTitle(title); 解释二:

    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>

    这时我们需要调用 CollapsingToolbarLayout 的 setTitle() 方法:

    CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.confirm_order_mail_layout); collapsingToolbarLayout.setTitle("My Title");

    * 这种情况没有使用过,来自下面的参考资料 *

    参考资料

    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
    转载请注明原文地址: https://ju.6miu.com/read-1309672.html
    最新回复(0)