为什么在CollapsingToolbarLayout中工具栏标题的字体和颜色不会改变?

huangapple go评论70阅读模式
英文:

Why the font and color of the toolbar title does not change in CollapsingToolbarLayout?

问题

我在使它工作方面遇到了问题。
它只能与默认字体(如无衬线字体)一起工作。任何导入的字体都可以与所有的文本视图一起工作,但在可折叠的工具栏中却不行。请帮忙。

**Styles.xml**

    <style name="AppBarTitleStyle" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
        <item name="android:fontFamily">@font/amatica_sc_reg</item>
        <item name="android:titleTextColor">@android:color/white</item>
    </style>

**CollapsingToolbar.xml**

 

       <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="308dp"
            android:background="@color/color_secondary"
            app:expanded="false">
    
            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/collapsingtoolbarid"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/colliding"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleMarginEnd="64dp"
                app:expandedTitleMarginStart="48dp"
                app:layout_scrollFlags="exitUntilCollapsed|scroll|snap"
                app:title="Learn Miwok"
                app:expandedTitleTextAppearance="@style/AppBarTitleStyle"
                app:collapsedTitleTextAppearance="@style/AppBarTitleStyle">
                    <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbarid"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin">
    
                </androidx.appcompat.widget.Toolbar>
    
            </com.google.android.material.appbar.CollapsingToolbarLayout>
    
        </com.google.android.material.appbar.AppBarLayout>
英文:

I have troubles with making it work.
It only works with default fonts like sans-seriff. Any imported fonts work with all textviews, however not with the collapsing toolbar. Please help.

Styles.xml

&lt;style name=&quot;AppBarTitleStyle&quot; parent=&quot;ThemeOverlay.AppCompat.Dark.ActionBar&quot;&gt;
    &lt;item name=&quot;android:fontFamily&quot;&gt;@font/amatica_sc_reg&lt;/item&gt;
    &lt;item name=&quot;android:titleTextColor&quot;&gt;@android:color/white&lt;/item&gt;
&lt;/style&gt;

CollapsingToolbar.xml

   &lt;com.google.android.material.appbar.AppBarLayout
        android:id=&quot;@+id/appBarLayout&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;308dp&quot;
        android:background=&quot;@color/color_secondary&quot;
        app:expanded=&quot;false&quot;&gt;

        &lt;com.google.android.material.appbar.CollapsingToolbarLayout
            android:id=&quot;@+id/collapsingtoolbarid&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot;
            android:background=&quot;@drawable/colliding&quot;
            app:contentScrim=&quot;?attr/colorPrimary&quot;
            app:expandedTitleMarginEnd=&quot;64dp&quot;
            app:expandedTitleMarginStart=&quot;48dp&quot;
            app:layout_scrollFlags=&quot;exitUntilCollapsed|scroll|snap&quot;
            app:title=&quot;Learn Miwok&quot;
            app:expandedTitleTextAppearance=&quot;@style/AppBarTitleStyle&quot;
            app:collapsedTitleTextAppearance=&quot;@style/AppBarTitleStyle&quot;&gt;
 &lt;androidx.appcompat.widget.Toolbar
                android:id=&quot;@+id/toolbarid&quot;
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;?attr/actionBarSize&quot;
                app:layout_collapseMode=&quot;pin&quot;&gt;

            &lt;/androidx.appcompat.widget.Toolbar&gt;

        &lt;/com.google.android.material.appbar.CollapsingToolbarLayout&gt;

    &lt;/com.google.android.material.appbar.AppBarLayout&gt;

答案1

得分: 1

你可以使用以下代码在 Java 中添加字体:

final Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/amatica_sc_reg");
collapsingToolbar.setCollapsedTitleTypeface(tf);
collapsingToolbar.setExpandedTitleTypeface(tf);
英文:

you can add font with java by using this below code

final Typeface tf = Typeface.createFromAsset(context.getAssets(), &quot;fonts/amatica_sc_reg&quot;);
collapsingToolbar.setCollapsedTitleTypeface(tf);
collapsingToolbar.setExpandedTitleTypeface(tf);

huangapple
  • 本文由 发表于 2020年5月4日 16:36:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/61588141.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定