将自定义字体应用于整个 Android 应用程序?
Applying custom font to the whole Android Application?
在你用否定comments/votes跳转之前,我会说明我已经阅读了`Whosebug上关于这个主题的每一个问题,并且应用了几乎所有的解决方案,但我仍然没有完全应用它。
所以,我问这个问题,因为我找到的几乎所有答案都是大约 4-5 年前的,我想知道现在是否有更好的解决方案。
我重复我的问题:有没有比列出的方法更好的方法来将整个应用程序中的整个字体系列覆盖为仅一种字体? (应用中使用的所有视图)?
提前致谢!
干杯!
最好的方法还是把你的.ttf
或.otf
字体文件放在assets文件夹中。然后派生一个自定义 TextView
class 并一劳永逸地修复它的字体,这样你就不必到处调用 setTypeface()
。
就这些了。
自定义 TextView Java Class
将这个class放入com.util包
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class CustomTextViewRegular extends TextView {
public CustomTextViewRegular(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
public CustomTextViewRegular(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public CustomTextViewRegular(Context context) {
super(context);
init(null);
}
private void init(AttributeSet attrs) {
if (attrs != null) {
// Set type face
Typeface myTypeface = Typeface.createFromAsset(getContext()
.getAssets(), "Helvetica_Neue.ttf");
setTypeface(myTypeface);
}
}
}
您必须在assets文件夹中有Helvetica_Neue.ttf,您可以使用还有其他字体。
如何在xml
中使用
<com.util.CustomTextViewRegular
android:id="@+id/txtCustom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textColor="#727272"
android:textSize="20dip" />
如果你想改变 字体你只需要改变字体输入Java 仅 class。
完成
我没有找到在应用程序上设置一种字体的解决方法,因此我求助于为每个 fragment/activities 中的每个视图调用 setTypeface()。谢谢,我很抱歉很长时间没有回答!我当之无愧的反对票,呵呵。
大家好!以后我会更加注意提问和对待回答用户的行为。
在你用否定comments/votes跳转之前,我会说明我已经阅读了`Whosebug上关于这个主题的每一个问题,并且应用了几乎所有的解决方案,但我仍然没有完全应用它。
所以,我问这个问题,因为我找到的几乎所有答案都是大约 4-5 年前的,我想知道现在是否有更好的解决方案。
我重复我的问题:有没有比列出的方法更好的方法来将整个应用程序中的整个字体系列覆盖为仅一种字体? (应用中使用的所有视图)?
提前致谢!
干杯!
最好的方法还是把你的.ttf
或.otf
字体文件放在assets文件夹中。然后派生一个自定义 TextView
class 并一劳永逸地修复它的字体,这样你就不必到处调用 setTypeface()
。
就这些了。
自定义 TextView Java Class
将这个class放入com.util包
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class CustomTextViewRegular extends TextView {
public CustomTextViewRegular(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
public CustomTextViewRegular(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public CustomTextViewRegular(Context context) {
super(context);
init(null);
}
private void init(AttributeSet attrs) {
if (attrs != null) {
// Set type face
Typeface myTypeface = Typeface.createFromAsset(getContext()
.getAssets(), "Helvetica_Neue.ttf");
setTypeface(myTypeface);
}
}
}
您必须在assets文件夹中有Helvetica_Neue.ttf,您可以使用还有其他字体。
如何在xml
中使用<com.util.CustomTextViewRegular
android:id="@+id/txtCustom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textColor="#727272"
android:textSize="20dip" />
如果你想改变 字体你只需要改变字体输入Java 仅 class。
完成
我没有找到在应用程序上设置一种字体的解决方法,因此我求助于为每个 fragment/activities 中的每个视图调用 setTypeface()。谢谢,我很抱歉很长时间没有回答!我当之无愧的反对票,呵呵。
大家好!以后我会更加注意提问和对待回答用户的行为。