How to increase font size of title in CollapsingTo

2019-07-23 21:39发布

问题:

I tried to add this on my Collapsing Toolbar Layout.

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

app_bar_home.xml

<android.support.design.widget.CoordinatorLayout
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="match_parent"
android:id="@+id/CoordinatorLayout"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:fitsSystemWindows="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/AppBarLayout"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_height="300dp"
        android:layout_width="match_parent"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleGravity="center|center_horizontal"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
    app:expandedTitleTextAppearance="@style/TextAppearance.AppCompat.Title">

        <ImageView
            app:backgroundTint="@color/text"
            android:id="@+id/canteen_logo"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:scaleType="fitCenter"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax"
            app:layout_collapseParallaxMultiplier="0.7"
            app:srcCompat="@drawable/canteen_logo1"
            />

        <android.support.v7.widget.Toolbar
            android:textAllCaps="true"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/toolbar_popup_style"
            app:layout_collapseMode="pin"/>
    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

And here's my Styles.xml:

<style name="TextAppearance.AppCompat.Title">
    <item name="android:textSize">32sp</item>
</style>

This is what I have in my Home.java and tried to remove them but I still couldn't get it fixed.

CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout)
            findViewById(R.id.collapsing_toolbar);
    final Typeface sp = Typeface.createFromAsset(this.getAssets(), "fonts/FuturaPTMedium.otf");
    final Typeface tf = Typeface.createFromAsset(this.getAssets(), "fonts/FuturaPTBook.otf");
    collapsingToolbar.setCollapsedTitleTypeface(sp);
    collapsingToolbar.setExpandedTitleTypeface(tf);
    setTitle("CANTEEN");

回答1:

Fixed it by adding this to the activity onCreate:

    CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout)
    findViewById(R.id.collapsing_toolbar);
    collapsingToolbar.setCollapsedTitleTextAppearance(R.style.CollapsedAppBar);
    collapsingToolbar.setExpandedTitleTextAppearance(R.style.ExpandedAppBar);

and adding this to styles.xml:

    <style name="CollapsedAppBar" parent="@android:style/TextAppearance">
    <item name="android:textSize">32sp</item>
</style>

<style name="ExpandedAppBar" parent="@android:style/TextAppearance">
    <item name="android:textSize">76sp</item>
</style>


回答2:

Also you can add the following into your app_bar_home.xml

<android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:collapsedTitleTextAppearance="@style/collapsed_app_bar"
        app:contentScrim="@color/colorPrimary"
        app:expandedTitleTextAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

Then Notice collapsedTitleTextAppearace and expandedTitleTextAppearance attributes. In my case I add the following into the style.xml under the values resource file:

<style name="collapsed_app_bar" parent="@android:style/TextAppearance">
    <item name="android:textSize">40sp</item>
    <item name="android:textColor">#f0f</item>
</style>

Hope this helps!!