栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在工具栏android中的标题上设置自定义字体

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在工具栏android中的标题上设置自定义字体

Update 2018(kotlin版本)

fun Toolbar.changeToolbarFont(){    for (i in 0 until childCount) {        val view = getChildAt(i)        if (view is TextView && view.text == title) { view.typeface = Typeface.createFromAsset(view.context.assets, "fonts/customFont") break        }    }}

像那样使用

toolBar.changeToolbarFont()

旧帖子

要在工具栏中使用自定义标题,您需要做的就是记住,工具栏只是一个精美的ViewGroup,因此您可以像这样添加自定义标题:

<android.support.v7.widget.Toolbar    android:id="@+id/toolbar_top"    android:layout_height="wrap_content"    android:layout_width="match_parent"    android:minHeight="?attr/actionBarSize"    android:background="@color/action_bar_bkgnd"    app:theme="@style/ToolBarTheme" >     <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Toolbar Title"        android:layout_gravity="center"        android:id="@+id/toolbar_title" />    </android.support.v7.widget.Toolbar>

这意味着您可以根据需要设置TextView的样式,因为它只是常规的TextView。因此,在您的活动中,您可以像这样访问标题:

Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title);

然后:

Typeface khandBold = Typeface.createFromAsset(BalrogApplication.getApplication().getAssets(), "fonts/Khand-bold.ttf");mTitle.setTypeface(khandBold);

*动态 *更新 版本

public static void changeToolbarFont(Toolbar toolbar, Activity context) {    for (int i = 0; i < toolbar.getChildCount(); i++) {        View view = toolbar.getChildAt(i);        if (view instanceof TextView) { TextView tv = (TextView) view; if (tv.getText().equals(toolbar.getTitle())) {     applyFont(tv, context);     break; }        }    }}public static void applyFont(TextView tv, Activity context) {    tv.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/customFont"));}

像那样使用

changeToolbarFont(findViewById(R.id.app_bar), this);


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/495925.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号