android Collapsingtoolbarlayout在需要时显示全文

时间:2016-04-26 15:56:28

标签: android title android-collapsingtoolbarlayout

您好我有一个使用Collapsingtoolbarlayout的Android应用程序。 Collapsingtoolbarlayout的标题将是上一个活动/片段中文章的标题(如何在需要时使Collapsingtoolbarlayout显示完整标题(调整大小字体,占用额外空间)?

目前,我使用这些代码。如何修改代码以正确显示标题?任何帮助都非常感激。感谢

Activity activity = this.getActivity();
        CollapsingToolbarLayout appBarLayout = (CollapsingToolbarLayout) activity.findViewById(R.id.toolbar_layout);
        if (appBarLayout != null) {
            appBarLayout.setTitle(topStory.getTitle());
            appBarLayout.setExpandedTitleTextAppearance(View.NO_ID);
            appBarLayout.setCollapsedTitleTypeface(Typeface.SANS_SERIF);
            appBarLayout.setTitleEnabled(true);
        }

2 个答案:

答案 0 :(得分:2)

要更改文本大小,您需要将其添加到CollapsingToolbarLayout:

   app:expandedTitleTextAppearance="@style/TextAppearance.AppCompat.Title">

或者在Java中:

mCollapsingToolbarLayout.setTitle(getTitle());
mCollapsingToolbarLayout.setExpandedTitleTextAppearance(android.R.stlye.);
mCollapsingToolbarLayout.setCollapsedTitleTextAppearance(android.R.style.);

要动态调整文章布局的大小,您有两个选择: 您将textSize设置为一个大小,这将允许每个可能的标题完全显示(如果您已经知道每个标题)

或者您检查标题有多少个字符并更改了CollapsingToolbarLayout的textAppeareance

答案 1 :(得分:1)

我简直不敢相信,四年后,我仍然在努力争取CollapsingToolbarLayout来解决这个问题。具体来说,我们需要展开后的CollapsingToolbarLayout的高度来包装展开后的标题的大小,而不包含任何图像或其他内容。

为了实现这一点,我不得不在TextView中创建一个伪造的CollapsingToolbarLayout来模仿扩展标题的外观和内容,以便CollapsingToolbarLayout根据其扩展高度来计算至此。该TextView仍然不可见,因此我们仍然可以在其折叠和展开的标题之间使用CollapsingToolbarLayout提供的动画。

以下是供您使用的XML:

<com.google.android.material.appbar.AppBarLayout 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:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:liftOnScroll="true">

    <com.google.android.material.appbar.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:collapsedTitleTextAppearance="@style/OmegaHeadingMedium"
        app:expandedTitleGravity="bottom"
        app:expandedTitleMarginBottom="@dimen/spacer_small"
        app:expandedTitleMarginEnd="@dimen/spacer_grid"
        app:expandedTitleMarginStart="@dimen/spacer_grid"
        app:expandedTitleMarginTop="?actionBarSize"
        app:expandedTitleTextAppearance="@style/OmegaHeadingLarge"
        app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
        app:maxLines="3"
        tools:title="Title">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            app:layout_collapseMode="pin"
            app:titleMarginEnd="@dimen/spacer_small" />

        <!-- This title is used to dynamically calculate the height
            of the collapsing toolbar. Styling should match that of
            the expanded title appearance. -->
        <TextView
            android:id="@+id/txt_fake_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginHorizontal="@dimen/spacer_grid"
            android:layout_marginTop="?actionBarSize"
            android:layout_marginBottom="@dimen/spacer_xsmall"
            android:textAppearance="@style/OmegaHeadingLarge"
            android:visibility="invisible"
            tools:text="Really really really really really really really really long title"
            tools:visibility="visible" />

    </com.google.android.material.appbar.CollapsingToolbarLayout>

</com.google.android.material.appbar.AppBarLayout>

(请记住,该XML中的样式,尺寸和其他属性仅用于演示)

在Kotlin中使用它的方式如下:

collapsing_toolbar.title = "Title"
txt_fake_title.text = "Title"