我正在做这件事:
toolbar = (Toolbar) findViewById(com.sports.unity.R.id.tool_bar);
setSupportActionBar(toolbar);
setTitle("hello");
我想为标题“hello”中的文本设置自定义字体。如何做到这一点?
typeFace= Typeface.createFromAsset(this.getAssets(), "fonts/myfont.ttf");
((TextView)toolbar.getChildAt(1)).setTypeface(typeFace);
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:layout_gravity="left"
android:id="@+id/toolbar_title"
android:textSize="20sp"/>
</android.support.v7.widget.Toolbar>
getSupportActionBar().setTitle(null);//Set the default to null
typeFace= Typeface.createFromAsset(getAssets(), "fonts/Raleway-Light.ttf");
toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
toolbarTitle.setTypeface(typeFace);
public class FontBindingAdapter
{
@BindingAdapter({"font"})
public static void setFont(Toolbar toolbar, String font)
{
for (int i = 0; i < toolbar.getChildCount(); i++) {
if (toolbar.getChildAt(i) instanceof AppCompatTextView) {
UIUtil.setFont(font, (AppCompatTextView) toolbar.getChildAt(i));
}
}
}
}
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/green"
app:font="@{`LatoRegular`}"
app:title="@string/setup_favorites"
app:titleTextColor="@color/white"/>
只需在您的工具栏 XML 中添加 textappearance,无需自定义:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:gravity="center"
android:minHeight="?attr/actionBarSize"
**app:titleTextAppearance="@font/montserrat_regular"**// apply your font
app:titleTextColor="@android:color/white" />
我仍然想使用工具栏标题方法,因此在工具栏XML元素中添加自定义TextView对我无效。相反,我使用以下方法查找TextView:
public static void applyFontForToolbarTitle(Activity context){
Toolbar toolbar = (Toolbar) context.findViewById(R.id.app_bar);
for(int i = 0; i < toolbar.getChildCount(); i++){
View view = toolbar.getChildAt(i);
if(view instanceof TextView){
TextView tv = (TextView) view;
Typeface titleFont = Typeface.
createFromAsset(context.getAssets(), "fonts/customFont");
if(tv.getText().equals(context.getTitle())){
tv.setTypeface(titleFont);
break;
}
}
}
}
如果你正在使用Google字体(例如Open Sans),你可以像下面这样将TextView添加为你的Toolbar的子项:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:fontFamily="@font/open_sans"/>
</android.support.v7.widget.Toolbar>
然后只需在属性菜单中为您的TextView设置fontFamily属性(下拉列表末尾的More Fonts选项)
android:fontFamily="@font/open_sans
谢谢 @gmetax,这是一个好观点。每个活动都访问textview是不好的。我们必须为每个活动编写以下代码:
TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title);
我们已经绑定了工具栏并使用了toolbar.setTitle()。因此,我扩展了Toolbar并重写了setTitle方法,如下所示:
@Override
public void setTitle(CharSequence title) {
super.setTitle("");
mTitle = (TextView) findViewById(R.id.toolbar_title);
if (mTitle != null) {
if (!TextUtils.isEmpty(title)) {
mTitle.setText(title);
}
}
}
toolbar.setTitle("bla bla");
您可以通过编程方式使用自定义字体工具栏,如下所示:
1)首先在资产文件夹中创建子目录“fonts”
2)将您的字体文件添加到“fonts”文件夹中。
toolbar.setTitle("Welcome");
Typeface fromAsset = Typeface.createFromAsset(getAssets(),"fonts/OpenSans-Light.ttf");
((TextView)toolbar.getChildAt(1)).setTypeface(fromAsset);
首先创建
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:titleTextAppearance="@style/DancingWhite"
app:popupTheme="@style/AppTheme.PopupOverlay"
>
<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>
第二个
在你的活动中,你可以这样访问标题:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
TextView mTitle = (TextView)
toolbar.findViewById(R.id.toolbar_title);
Typeface font = Typeface.createFromAsset(getAssets(), "Mukta- Regular.ttf");
mTitle.setTypeface(font);
下载自定义字体并将其保存在res文件夹内的font文件夹中。 由于工具栏是一个viewGroup,我们可以将textView放置在工具栏内,并按以下方式使用
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
>
<TextView
android:id="@+id/toolbar_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Toolbar"
android:gravity="center"
android:fontFamily="@font/roboto_regular" <--- custom font
android:textColor="@color/white"
android:textStyle="bold"
android:textAppearance="@style/Base.TextAppearance.Widget.AppCompat.Toolbar.Title"
/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
这种方法也用于使字体加粗。
getSupportActionBar().setDisplayShowTitleEnabled(false);
。 - zayn1991