Android 工具栏如何显示两行标题?

20

我希望能够创建一个双倍高度的操作栏(就像Material Design指南中的示例),但标题可能会有两行文本。所使用的标题是动态的,并且取决于页面上显示的项目。如果只有一行,则应该看起来像普通操作栏,如果有两行,则文本应该换行并放在新行上。操作按钮应该始终与文本的顶部对齐,无论是一行还是两行。

我了解到Toolbar是实现动态操作栏的新方式,因此我认为这可能是正确的方法(我知道有一些答案可以覆盖旧的操作栏标题TextView以使其具有两行,但这似乎不是我想要的)。我正在使用Android 4.4+,因此需要使用appcompat v7库来创建Toolbar。

当查看代码时,我发现标题TextView被限制为单行(https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/v7/appcompat/src/android/support/v7/widget/Toolbar.java - 包含代码mTitleTextView.setSingleLine();)。

然后我想到,由于底部有单行副标题,而且我们可以更改该文本的格式,我可以将其格式与标题文本相同,如果需要,将我的标题分成两个部分。我编写了一些基本代码来拆分字符串,但它依赖于能够确定是否需要拆分。实际上,我发现toolbar.isTitleTruncated()始终返回false(直接从我的toolbar对象访问和从getSupportActionBar().isTitleTruncated()访问都是如此)。

我尝试了这里的答案,在Toolbar中包装一个TextView,但是我无法使TextView与Toolbar操作按钮对齐。如果我正确地将其与单行对齐(使用顶部填充),则对于双行,它是错误的,反之亦然。以下是我的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/view_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
    android:id="@+id/fragment_pager"
    android:name="com.example.ProductPagerFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.v7.widget.Toolbar
    android:id="@+id/product_details_toolbar"
    style="@style/style.toolbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/double_height_toolbar"
    android:minHeight="?attr/actionBarSize"
    android:gravity="top"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >

    <TextView
        android:id="@+id/product_details_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:paddingTop="@dimen/padding_normal"
        style="@android:style/TextAppearance.Holo.Widget.ActionBar.Title.Inverse"
        android:maxLines="2" />

</android.support.v7.widget.Toolbar>

有人有什么想法吗?这真的是违反Android指南的事情吗,以至于它应该如此困难吗?

更新:添加演示图像:

应该是这样的:

应该是这样的

现在看起来是这样的:

现在看起来是这样的


我为isTitleTruncated()无法正常工作的问题创建了https://code.google.com/p/android/issues/detail?id=81987。 - gkee
2个回答

24

7
嗯,这对我来说很好用。

屏幕截图


<android.support.v7.widget.Toolbar
        android:layout_height="200dp"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:gravity="top"
        android:background="?attr/colorPrimaryDark"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

    <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Title"
            android:textAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"/>

</android.support.v7.widget.Toolbar>

2
如果我只有一行文本,这对我来说很好用。但是如果我有两行,那么对齐就会出现问题。请尝试使用较长的标题。 - gkee
请勿在此解决方案中使用toolbar.setTitle()。获取工具栏视图内的TextView并通过编程方式设置其文本。 - Erdi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接