我想将工具栏标题的font-family更改为sans-serif-smallcaps
。我该怎么做?
我的AppTheme
有父Theme.AppCompat.Light.DarkActionBar
编辑:工具栏XML:
<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" />
答案 0 :(得分:7)
我的布局页面:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<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" />
</android.support.design.widget.AppBarLayout>
转到 styles.xml ,然后添加字体(这在Android Studio 3.0中有效)
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" >
<item name="android:fontFamily">@font/handlee_regular</item>
/// change your font
</style>
答案 1 :(得分:2)
#使用默认的Android字体系列:
在您的TextView
XML中,添加一个子Title
以显示TextView
。使用属性android:fontFamily="sans-serif-smallcaps"
<android.support.v7.widget.Toolbar
android:id="@+id/oolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_collapseMode="pin"
app:popupTheme="?attr/colorPrimaryDark" >
<TextView
android:id="@+id/custom_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stack Overflow"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
android:fontFamily="sans-serif-smallcaps" />
</android.support.v7.widget.Toolbar>
<强>工具栏:强>
.../app/src/main/assets/fonts/YOUR_CUSTOM_FONT.ttf
#使用外部字体:
将外部字体文件放入位置:Activity
在custom font
中添加以下代码,将Toolbar
设置为public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// Custom title
TextView textCustomTitle = (TextView) findViewById(R.id.custom_title);
// Custom font
Typeface customFont = Typeface.createFromAsset(this.getAssets(), "fonts/Exo2-BoldItalic.ttf");
// Set
textCustomTitle.setTypeface(customFont);
setSupportActionBar(toolbar);
}
.......
..............
}
标题。
<强> MainActivity.java 强>
l_query := REPLACE(l_query,'<REF_D>', REF_D)
<强>输出:强>
希望这会有所帮助〜
答案 2 :(得分:0)
工作正常。
mToolbar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
View view = mToolbar.getChildAt(0);
if (view != null && view instanceof TextView) {
TextView title = (TextView) view;
AssetManager mgr = getAssets();
Typeface tf = Typeface.createFromAsset(mgr, "ttf.ttf");//Font file in /assets
title.setTypeface(tf);
mToolbar.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
});
答案 3 :(得分:0)
将您的字体文件(.ttf)放在assets文件夹中,并在Activity类中编写以下代码。
Typeface font = Typeface.createFromAsset(getContext().getAssets(), "sansSmallCaps.ttf");
TextView text = (TextView) findViewById(R.id.toolbartitle);
text.setTypeface(font);
<强>更新强>
你正确使用getChildAt(0)获取textview但我不知道为什么字体不适用于标题。但我猜你需要在设置setSupportActionBar(myToolbar);
Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar);
myToolbar.setTitle("xyz");
TextView tv=(TextView) myToolbar.getChildAt(0);
tv.setTypeface(Typeface.SANS_SERIF);
setSupportActionBar(myToolbar);
答案 4 :(得分:0)
创建一个SpannableString
对象并从资产中传递字体路径。检查下面的工作
SpannableString toolbarTitle = new SpannableString(getActionBar().getTitle());
String toolbarFont = getResources().getString(R.string.circular_bold);
CustomTypefaceSpan toolbarTypefaceSpan = new CustomTypefaceSpan(toolbarFont, this);
toolbarTitle.setSpan(toolbarTypefaceSpan, 0, toolbarTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
getActionBar().setTitle(toolbarTitle);
此处R.string.circular_bold
是
<string name="circular_bold">font/CircularStd-Bold.ttf</string>
字体位于Asset/font
文件夹中,如下图所示
以下CustomFontHelper
类有助于将Typeface
设置为文本元素 -
public class CustomFontHelper {
/**
* Changing font of Paint element
* @param paint text element of which font needs to be changed
* @param font
* @param context
*/
public static void setCustomFont(Paint paint, String font, Context context) {
if (font == null) {
return;
}
Typeface typeface = FontCache.get(font, context);
if (typeface != null) {
paint.setTypeface(typeface);
}
}
}
CustomTypefaceSpan
class是将font应用于文本元素的实际类。
public class CustomTypefaceSpan extends TypefaceSpan {
private String font;
private Context context;
public CustomTypefaceSpan(@NonNull String font, @NonNull Context context) {
super("");
this.font = font;
this.context = context;
}
@Override
public void updateDrawState(TextPaint ds) {
CustomFontHelper.setCustomFont(ds, font, context);
}
@Override
public void updateMeasureState(TextPaint paint) {
CustomFontHelper.setCustomFont(paint, font, context);
}
}
FontCache
类用于缓存字体,因此可以重复使用相同的字体
公共类FontCache {
private static Hashtable<String, Typeface> fontCache = new Hashtable<>();
/**
* Gets the typeface from Asset folder
* @param name path to the font within asset folder
* @param context context of the view
* @return
*/
public static Typeface get(String name, Context context) {
Typeface tf = fontCache.get(name);
if (tf == null) {
try {
tf = Typeface.createFromAsset(context.getAssets(), name);
} catch (Exception e) {
return null;
}
fontCache.put(name, tf);
}
return tf;
}
}