我正在做这件事:
toolbar = (Toolbar) findViewById(com.sports.unity.R.id.tool_bar);
setSupportActionBar(toolbar);
setTitle("hello");
我想为标题“hello”中的文本设置自定义字体。如何做到这一点?
自从 android.support.v7.appcompat 24.2
版本,Toolbar
组件就有了 setTitleTextAppearance
方法,你可以不需要外部的 textview
组件来设置其字体。
在 styles.xml 中创建新样式。
<style name="RobotoBoldTextAppearance">
<item name="android:fontFamily">@font/roboto_condensed_bold</item>
</style>
并使用它
mToolbar.setTitleTextAppearance(this, R.style.RobotoBoldTextAppearance);
更新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()
旧文章
要在工具栏中使用自定义标题,您只需要记住工具栏只是一个高级视图组,因此您可以像这样添加自定义标题:
<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);
toolbar.setTitle()
将会徒劳无功,我是对的吗? - Mehmedview.text == title
更改为 view.text == title.toString()
。 - Edmund Johnson我仍然希望使用工具栏标题方法(也不想拥有自定义的工具栏类),因此在工具栏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(toolbar.getTitle())){
tv.setTypeface(titleFont);
break;
}
}
}
}
TextView
的情况下设置自定义字体。 - Ícaro我写了一篇文章,详细介绍了解决方案。以下是基本步骤:
styles.xml
中定义一个主题:<style name="ToolbarTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="android:fontFamily">@font/fancy-font</item>
</style>
Toolbar
布局中设置该主题:<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:theme="@style/ToolbarTheme"/>
res/font
文件夹中存储了一个.ttf
字体文件:<style name="**TextAppearance.TabsFont**" parent="**android:TextAppearance**">
<item name="android:fontFamily">font/rmedium</item>
<item name="android:textSize">18sp</item>
</style>
3.现在打开布局并在工具栏标签中添加 app:titleTextAppearance="@style/TextAppearance.TabsFont",如下所示。
<android.support.v7.widget.Toolbar
android:id="@+id/toolbarMain"
app:title="@string/time_amp_date"
app:titleTextAppearance="@style/TextAppearance.TabsFont"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:theme="@style/AppTheme"
app:elevation="2dp" />
完成了DONE。现在,如果您运行应用程序,您可以看到字体设置为您的工具栏。
Placement of Fonts:
Go to the java file of the activity whose font you have to customize. I customized mainActivity here.
for(int i = 0; i < toolbar.getChildCount(); i++)
{ View view = toolbar.getChildAt(i);
if(view instanceof TextView) {
TextView textView = (TextView) view;
Typeface myCustomFont=Typeface.createFromAsset(getAssets(),"font/Balker.ttf");
textView.setTypeface(myCustomFont); }
}
app:titleTextAppearance="@style/ToolbarTextAppearance"
- murt两种方式可以在工具栏标题上使用自定义字体
解决方案1 [静态方法]
1)在styles.xml中定义一个主题:
<style name="ToolbarTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="android:fontFamily">@font/ubuntu</item>
</style>
2) 在您的工具栏布局中设置该主题:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:theme="@style/ToolbarTheme"/>
解决方案 2:[ 动态 ]
Kotlin
fun Toolbar.titleFont(){
for (i in 0 until childCount) {
val view = getChildAt(i)
if (view is TextView && view.text == title) {
view.typeface = Typeface.createFromAsset(context.assets,"fonts/customfont.ttf")
break
}
}
}
然后像这样使用它:
toolBar.titleFont()
如果你只想改变工具栏标题的字体,不需要使用外部textview,因为android.support.v7.appcompat 24.2
Toolbar
有一个方法setTitleTextAppearance
,你可以像下面这样设置它的字体。
在styles.xml中创建一个新的样式。
<style name="CamptonBookTextAppearance">
<item name="android:fontFamily">@font/campton_book</item>
</style>
并使用它
mToolbar.setTitleTextAppearance(this, R.style.CamptonBookTextAppearance);
Toolbar toolbar = (Toolbar) findViewById(com.sports.unity.R.id.tool_bar);
TextView tv = getToolBarTextView();
tv.settext("Hello");
private TextView getToolBarTextView() {
TextView titleTextView = null;
try {
Field f = mToolBar.getClass().getDeclaredField("mTitleTextView");
f.setAccessible(true);
titleTextView = (TextView) f.get(mToolBar);
Typeface font = Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/mfont.ttf");
titleTextView.setTypeface(font);
} catch (NoSuchFieldException e) {
} catch (IllegalAccessException e) {
}
return titleTextView;
}
<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/primary">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/signup"
android:textSize="22sp" />
</android.support.v7.widget.Toolbar>
android:fontFamily
的属性。<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/primary">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto_slab"
android:text="@string/signup"
android:textColor="@color/secondaryBackground"
android:textSize="22sp" />
</android.support.v7.widget.Toolbar>
parent="TextAppearance.Widget.AppCompat.Toolbar.Title"
。因为标题的大小对我来说太小了。 - Kevin van Mierloapp:titleTextAppearance="@style/ToolbarTextAppearance"
- Jimmy ALejandro