【问题标题】:Android Appcompat Toolbar Title not shown correctlyAndroid Appcompat 工具栏标题未正确显示
【发布时间】:2017-04-07 07:23:02
【问题描述】:

我正在使用来自版本支持库 25.0.1 的支持工具栏,它随机显示如下:

现在这是随机的,我不知道如何重现。有时显示正常,有时显示不正确。

这就是我在片段中使用工具栏的方式:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    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="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/Theme.App.Toolbar"
    android:elevation="@dimen/toolbar_elevation"
    app:layout_scrollFlags="scroll|enterAlways|snap"
    app:popupTheme="@style/Theme.App.Toolbar.Popup"/>

<style name="Theme.App.Toolbar">
    <item name="android:textColorPrimary">@color/white</item>
    <item name="actionMenuTextColor">@color/white</item>
    <item name="android:textColorSecondary">@color/white</item>
    <item name="android:textColorHint">@color/white</item>
</style>

片段:

toolbar.setTitle("Story title");
appCompatActivity().setSupportActionBar(toolbar);

从屏幕截图中可以看出,工具栏试图放置字幕,但我没有。

这是否发生在其他人身上,这是此问题的已知错误,对此有任何解决方案。

【问题讨论】:

    标签: android android-toolbar android-design-library


    【解决方案1】:

    到目前为止,我唯一的解决方案是使用支持库版本25.0.0

    欢迎其他解决方案或更新

    【讨论】:

      【解决方案2】:

      尝试像这样使用它:

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/tb_toolbar"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="@color/colorPrimary"
          android:theme="@style/Theme.MyTheme"
          tools:ignore="NewApi">
      
          <TextView
              android:id="@+id/tv_title_toolbar"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center"
              android:textColor="@android:color/white"
              android:textSize="18sp"
              />
      </android.support.v7.widget.Toolbar>
      

      这里可以设置标题重心向左。

      然后在代码中这样访问:

      toolbar = (Toolbar) findViewById(R.id.tb_toolbar);
      tvToolbarTitle = (TextView) toolbar.findViewById(R.id.tv_title_toolbar);
      tvToolbarTitle.setText(title);
      

      【讨论】:

      • 我不想自定义它,想象一下对于现有项目,您将不得不到处设置文本标题而不是工具栏.setTitle。我相信在未来这将得到修复
      • 是的,同时您必须坚持使用旧版本的支持库 25.0.0 或者您可以使用上述解决方案自定义工具栏
      猜你喜欢
      • 1970-01-01
      • 2016-03-06
      • 1970-01-01
      • 1970-01-01
      • 2015-03-31
      • 1970-01-01
      • 2015-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多