我正在尝试找出使用自定义字体作为工具栏标题的正确方法,并将其置于工具栏中(客户端要求).
目前,我正在使用旧的ActionBar,我将标题设置为空值,并使用setCustomView自定义字体TextView并使用ActionBar.LayoutParams将其居中.
有没有更好的方法呢?使用新工具栏作为我的ActionBar.
android android-layout android-theme android-styles android-toolbar
我在我的应用程序中使用Roboto light字体.要设置字体,我要添加android:fontFamily="sans-serif-light"到每个视图.有没有办法将Roboto字体声明为整个应用程序的默认字体系列?我尝试过这样但它似乎没有用.
<style name="AppBaseTheme" parent="android:Theme.Light"></style>
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:fontFamily">sans-serif-light</item>
</style>
Run Code Online (Sandbox Code Playgroud) 我想为我正在创建的Android应用程序使用自定义字体.
我可以从Code中单独更改每个对象的字体,但我有数百个.
所以,
是否可以在应用程序的每个控件中设置任何自定义字体?而不一定是运行时?(即,如果可能的话,从xml中获取,或者在JAVA文件中仅用于整个应用程序一次)
我可以从这段代码设置一个控件的字体.
public static void setFont(TextView textView) {
Typeface tf = Typeface.createFromAsset(textView.getContext()
.getAssets(), "fonts/BPreplay.otf");
textView.setTypeface(tf);
}
Run Code Online (Sandbox Code Playgroud)
这段代码的问题是应该为每个控件调用它.我想一次调用这个或任何类似的方法,或者如果可能的话,在xml中设置属性.可能吗?
是否可以在Android应用程序中设置自定义字体?
我尝试了这里发布的内容,但我不知道我的extends Application课程在哪里......
有帮助吗?
编辑:
我尝试了以下方法:
添加一个扩展自的新类 Application
从我的电话中调用这个新课程AndroidManifest.xml.
我按照我的风格添加了它.
MyApp.java:
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
FontsOverride.setDefaultFont(this, "DEFAULT", "raleway_regular.ttf");
// This FontsOverride comes from the example I posted above
}
}
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml中:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:name=".MyApp"
android:theme="@style/AppTheme">
....
Run Code Online (Sandbox Code Playgroud)
styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:fontFamily">default</item>
</style>
Run Code Online (Sandbox Code Playgroud)
但我的字体仍然没有变换......任何想法?
然后MyApp调用该类.但对我的字体没有影响......
EDIT2:我意识到在为按钮设置自定义样式后,我的按钮会应用自定义字体.这是我的自定义按钮样式:
<style name="MyButtonStyle" parent="Widget.AppCompat.Button">
<item name="textAllCaps">false</item>
<item name="android:textAllCaps">false</item>
</style>
Run Code Online (Sandbox Code Playgroud)
以下是它现在的样子:
所以:我的按钮正在应用样式,但不是TextView.有关为什么我的自定义字体没有应用于应用程序中的所有项目的任何想法?
我开发了一个android项目.在这个项目中,文本字体默认为android:sans.
现在我想将整个项目的默认文本字体替换为roboto字体.
我怎样才能做到这一点?
以下用于设置自定义字体的代码会降低整个应用程序的速度.我如何修改它以避免内存泄漏,提高速度和管理内存?
public class FontTextView extends TextView {
private static final String TAG = "FontTextView";
public FontTextView(Context context) {
super(context);
}
public FontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public FontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.FontTextView);
String customFont = a.getString(R.styleable.FontTextView_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = …Run Code Online (Sandbox Code Playgroud) android android-emulator android-intent android-layout android-listview
可能重复:
是否可以为整个应用程序设置字体?
我需要为整个应用程序设置自定义字体(.ttf格式),我该怎么办?如果可能,从Manifest或XML将是最好的选择
我想应用一些字体说Times New Roman只适用于我的应用程序. 不是对整个系统而是对特定的观点.据我所知
要将字体应用于特定视图,我们将字体文件存储在资产文件夹中,并按如下方式进入应用程序.
1] Typeface mFace = Typeface.createFromAsset(getContext().getAssets(),
"fonts/samplefont.ttf");
textView.setTypeface(mFace);
2] To apply font to whole application I can replace the DroidSans.ttf file with my font file.
Run Code Online (Sandbox Code Playgroud)
我可以使用第一种方式将字体应用到我的应用程序,但它不是一个好的解决方案,因为我需要在任何地方进行修改,如果有更好的方法,我不想这样做.
我正在使用这样的格式化货币字符串获取一些JSON数据
?35
Run Code Online (Sandbox Code Playgroud)
但是,我注意到在Nexus 5(Lollipop)上它显示正确,但其他手机,如HTC one mini和Samsung GT-I9505,它显示一个空白字符.
我试图研究这个问题,除了在XML布局文件中,我找不到解决方案,确保该行存在
<?xml version="1.0" encoding="utf-8"?>
Run Code Online (Sandbox Code Playgroud)
但我仍然有同样的问题
请帮忙
编辑2015年5月15日
加载自定义字体NotoSans(请注意我知道这会泄漏内存,但它只是一个快速测试)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView ruble = (TextView)findViewById(R.id.ruble);
Typeface myFont = Typeface.createFromAsset(getAssets(),"fonts/NotoSans-Regular.ttf");
ruble.setTypeface(myFont);
}
Run Code Online (Sandbox Code Playgroud)
strings.xml中定义的俄罗斯卢布符号(注意我尝试了所有)
<string name="rubleSymbolJava">\u20BD</string>
<string name="rubleSymbolHTML">₽</string>
<string name="rubleSymbolHTMLHex">₽</string>
Run Code Online (Sandbox Code Playgroud)
同样的问题,在老式手机上显示为方形但适用于Android 5.0但没有更旧.
已解决请参阅我的回答