Android工具栏标题中有两行?

gke*_*kee 14 android android-layout android-actionbar android-toolbar

我希望有一个双倍高度的操作栏(如材料指南示例中所示),但标题中有(可能)2行文本.我正在使用的标题是动态的,取决于页面中显示的项目.如果有一行它应该看起来像一个普通的动作栏,如果有两行,则文本应该断开并下拉到一个新行.无论是否有一行或两行,操作按钮都应与文本的顶行保持对齐.

我读到工具栏是动态的新动态方式所以我认为这可能是要走的路(我知道那里有答案可以让你覆盖旧的操作栏标题textview使其成为2行,但是看起来不像我想要的那样).我使用的是Android 4.4+,因此我需要将appcompat v7库用于工具栏.

我发现看,标题的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(从我直接访问的工具栏对象和来自getSupportActionBar().isTitleTruncated()).

我想答案在这里,以一个包裹TextView内的Toolbar,但我不能让TextView我做,用工具栏操作按钮排列.如果我为一条线正确对齐(使用顶部填充),那么双线是错误的,反之亦然.这是我的代码:

<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>
Run Code Online (Sandbox Code Playgroud)

有没有人有任何想法?这是否真的与Android指南相反,应该如此困难?

更新:添加图像用于演示目的:

应该如何看待:

应该怎么样

它现在看起来如何:

看起来如何

小智 17

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(getResources().getString(R.string.myTitle));
toolbar.setSubtitle(getResources().getString(R.string.mySubTitle));
setSupportActionBar(toolbar);
Run Code Online (Sandbox Code Playgroud)

教程:

ToolBar示例

将标题,副标题,颜色添加到操作栏/工具栏


Chr*_*nes 5

嗯,对我来说很好。

屏幕截图

<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>
Run Code Online (Sandbox Code Playgroud)

  • 如果我有一行文本,这对我来说很好用。如果我有两个,那么对齐就会变得一团糟。尝试使用更长的标题。 (2认同)