【问题标题】:Title in expanded CollapsingToolbarLayout not displayed correctly展开的 CollapsingToolbarLayout 中的标题未正确显示
【发布时间】:2017-07-31 20:56:17
【问题描述】:

所以,我的项目中的CollapsingToolbarLayout 遇到了一个奇怪的问题。在我的活动开始后,我的工具栏标题如下所示:

折叠后的布局是这样的:

示例中的原始标题文本为:“UPC VONALKODOS TERMEK

我认为展开状态下的标题应该比折叠状态下更长(有足够的空间)。这是我活动的 xml 的样子:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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:fitsSystemWindows="true"
    app:theme="@style/PolarThemeNoActionBar">
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_below="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_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/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="142dp"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginBottom="20dp"
            app:expandedTitleTextAppearance="@style/ExpandedText">
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                android:minHeight="?attr/actionBarSize"
                app:layout_collapseMode="pin"/>
            </android.support.design.widget.CollapsingToolbarLayout>
        <android.support.design.widget.TabLayout
            android:id="@+id/tablayout"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_below="@+id/toolbar"
            android:minHeight="?attr/actionBarSize"
            android:gravity="bottom"
            android:background="?attr/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:tabIndicatorColor="?attr/colorPrimaryDark"/>
        </android.support.design.widget.AppBarLayout>
    </android.support.design.widget.CoordinatorLayout>

我的 res/style/ExpandedText 看起来像:

<style name="ExpandedText" parent="android:TextAppearance">
    <item name="android:textColor">@android:color/white</item>
    <item name="android:textSize">20sp</item>
    </style>

支持库版本:25.1.1。 电话:Nexus 5 Android 版本:6.0.1(库存)

我的问题:为什么标题在展开状态的末尾有点,而不是填充空间以显示更多内容?

[EDIT 1]问题仍然存在于支持库版本 25.3.0

【问题讨论】:

  • 看起来这种行为是在相对较新的库版本中引入的。我觉得这看起来很奇怪,所以我拉了一个旧项目,它的行为与您对 23.4.0 版本的预期一样。即,标题完全显示为展开状态,而不是像我之前提到的那样根据折叠的大小和区域被椭圆化。我不确定哪个版本带来了变化,但看看源代码的 git blame,似乎这是在去年年中完成的,以修复标题与图标等重叠的边缘情况。如果可能的话,您可以提出问题并暂时恢复。
  • @Mike M. 感谢您的评论。我认为您是对的,在我的应用程序的旧版本中,CollapsingToolbarLayout 标题看起来不错:我认为您应该发布此评论作为答案。

标签: android android-design-library android-collapsingtoolbarlayout


【解决方案1】:

CollapsingToolbarLayout 使用辅助类 - CollapsingTextHelper - 来绘制和动画其标题。在撰写本文时,此类的最新版本将展开标题的可用宽度限制为基于折叠状态下可用宽度的大小,并按状态文本大小的比例缩放。

相关来源cmets:

// If the scaled down size is larger than the actual collapsed width, we need to
// cap the available width so that when the expanded text scales down, it matches
// the collapsed width

这显然是为了解决标题与其他 Toolbar 内容重叠的一些极端情况,如 the relevant commit 上的注释中所述。

修复 CollapsingToolbarLayout 显示在图标上

CTL 缩放它的标题,这在大多数情况下都有效。 虽然标题可以是边缘情况 绘制在 Toolbars 内容上,即图标。

这个 CL 修复了折叠的边缘情况 和扩展的文本大小在大小上相似,这 意味着发生有限/没有缩放 滚动时。在这种情况下,我们需要限制 展开时任何可用的宽度,因此它 “缩放”以匹配折叠时的折叠宽度。

通常情况下,我会使用反射和其他技巧来修改 View 类以修改它们的行为,但在这种情况下,给定的设置需要一些非常繁重的工作。 helper 类通常不能在库包之外访问,它的实例在CollapsingToolbarLayout 中是私有的,并且大小计算是在一个私有的辅助方法中执行的,其中大部分是局部变量。

如果有可能,最简单的解决方案是恢复到此修复之前发布的库版本。我尚未确定带来此更改的确切版本,不幸的是,support library revision history 似乎没有提及它。但是,该提交是在去年年中(2016 年)做出的,所以可能在 24.0.0 版左右,或者稍晚一点。我可以验证该行为在 23.4.0 中不存在。

如果您愿意,您当然可以为此 file a bug report,但不能保证他们会认为这是一个错误。除了this tangentially-related one 抱怨椭圆化是该更改的副作用之外,我没有发现任何先前提交的问题。

【讨论】:

  • 精心制作。
【解决方案2】:

我编辑你的代码看看这可能是你喜欢这个

<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true">

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/tablayout"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_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/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="142dp"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginBottom="20dp"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleTextAppearance="@style/ExpandedText"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            app:contentInsetStartWithNavigation="0dp"
            app:layout_collapseMode="pin" />
    </android.support.design.widget.CollapsingToolbarLayout>

    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_below="@+id/toolbar"
        android:background="?attr/colorPrimary"
        android:gravity="bottom"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:tabIndicatorColor="?attr/colorPrimaryDark" />
</android.support.design.widget.AppBarLayout>

这是活动类

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;
private CollapsingToolbarLayout collapsingToolbar;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.answer2);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
    collapsingToolbar.setTitle("UPC VONALKODOS TERMEK");
    collapsingToolbar.setExpandedTitleTextAppearance(R.style.ExpandedAppBar);
    collapsingToolbar.setCollapsedTitleTextAppearance(R.style.CollapsedAppBar);
}

}

这是 res/style/:

<style name="CollapsedAppBar" parent="@android:style/TextAppearance.Medium">
    <item name="android:textSize">16sp</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:textStyle">normal</item>
</style>
<style name="ExpandedAppBar" parent="@android:style/TextAppearance.Medium">
    <item name="android:textSize">20sp</item>
    <item name="android:textStyle">bold</item>
</style>

【讨论】:

    【解决方案3】:

    您可以关闭省略号。将此添加到您的 TextAppearance 样式中:

    <item name="android:ellipsize">none</item>
    

    如果有必要,您还可以手动更改通过向样式添加宽度来创建的 textview 的宽度

    <item name="android:width">300dp</item>
    

    【讨论】:

    • 感谢您的回答,但没有奏效。我的 CollapsingToolbar 标题看起来一样。
    猜你喜欢
    • 1970-01-01
    • 2015-09-24
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 2017-03-12
    相关资源
    最近更新 更多