简体   繁体   中英

How can I change the color of a CollapsibleToolbar title?

So, I'm struggling for a few days meddling with lots of different attributes, but still couldn't figure out how to change the text color of a Collapsing toolbar's title. Any takes on this? Here's a very simple layout I've been experimenting with. The app:titleTextColor="@color/colorPrimary" produces no effect.

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
    tools:context=".MainActivity">
    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/appBarLayout"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar">
        
        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|snap|exitUntilCollapsed"
            app:contentScrim="@color/colorPrimary"
            app:expandedTitleGravity="center|top"
            app:title="@string/app_name"
            >
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:src="@drawable/zebra"
                android:scaleType="centerCrop"
                android:contentDescription="@string/zebras"
                />
            <androidx.appcompat.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
                app:titleTextColor="@color/colorPrimary"

            />
        </com.google.android.material.appbar.CollapsingToolbarLayout>
    </com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Add styles in styles.xml

 <style name="CollapsedAppBarText" parent="@android:style/TextAppearance.Medium">
    <item name="android:textSize">16sp</item>
    <item name="android:textColor">@color/blue</item>
    <item name="android:textStyle">normal</item>
</style>

then Activity/ fragment use like this

collapsingToolbar = findViewById<View>(R.id.collapsing_toolbar) as CollapsingToolbarLayout
    

collapsingToolbar?.setCollapsedTitleTextAppearance(R.style.CollapsedAppBarText)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM