英文:
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
<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>
答案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(), "fonts/amatica_sc_reg");
collapsingToolbar.setCollapsedTitleTypeface(tf);
collapsingToolbar.setExpandedTitleTypeface(tf);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论