在Android中为整个应用程序更改XML中的Roboto字体
我需要在XML中更改我的应用程序的Roboto字体,我想在运行时更改Roboto字体。我如何做到这一点?您可以使用资产文件来实现这一点。将字体转储到在Android中为整个应用程序更改XML中的Roboto字体,android,android-xml,android-fonts,Android,Android Xml,Android Fonts,我需要在XML中更改我的应用程序的Roboto字体,我想在运行时更改Roboto字体。我如何做到这一点?您可以使用资产文件来实现这一点。将字体转储到资产文件中 并使用字体类相应地设置字体样式 Typeface style = Typeface.createFromAsset(asm, "fonts/Roboto-Bold.ttf"); view.setTypeFace(style); 注意,在这里,Roboto字体保存在另一个目录中,即资源中的字体 为了更好地实践,您可以创建一个单独的类来扩展
资产
文件中
并使用字体
类相应地设置字体样式
Typeface style = Typeface.createFromAsset(asm, "fonts/Roboto-Bold.ttf");
view.setTypeFace(style);
注意,在这里,Roboto字体保存在另一个目录中,即资源中的字体
为了更好地实践,您可以创建一个单独的类来扩展所需的视图。除了包名之外,您还可以访问xml布局中创建的视图
public class BoldTextView extends TextView{
public BoldTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public BoldTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public BoldTextView(Context context) {
super(context);
init();
}
private void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"fonts/Roboto-Bold.ttf");
setTypeface(tf);
}
}
布局中对此类的引用:
com.your.packagename.BoldTextView
您可以为此使用资产文件。将字体转储到
资产
文件中
并使用字体
类相应地设置字体样式
Typeface style = Typeface.createFromAsset(asm, "fonts/Roboto-Bold.ttf");
view.setTypeFace(style);
注意,在这里,Roboto字体保存在另一个目录中,即资源中的字体
为了更好地实践,您可以创建一个单独的类来扩展所需的视图。除了包名之外,您还可以访问xml布局中创建的视图
public class BoldTextView extends TextView{
public BoldTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public BoldTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public BoldTextView(Context context) {
super(context);
init();
}
private void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"fonts/Roboto-Bold.ttf");
setTypeface(tf);
}
}
布局中对此类的引用:
com.your.packagename.BoldTextView
您可以为此使用资产文件。将字体转储到
资产
文件中
并使用字体
类相应地设置字体样式
Typeface style = Typeface.createFromAsset(asm, "fonts/Roboto-Bold.ttf");
view.setTypeFace(style);
注意,在这里,Roboto字体保存在另一个目录中,即资源中的字体
为了更好地实践,您可以创建一个单独的类来扩展所需的视图。除了包名之外,您还可以访问xml布局中创建的视图
public class BoldTextView extends TextView{
public BoldTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public BoldTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public BoldTextView(Context context) {
super(context);
init();
}
private void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"fonts/Roboto-Bold.ttf");
setTypeface(tf);
}
}
布局中对此类的引用:
com.your.packagename.BoldTextView
您可以为此使用资产文件。将字体转储到
资产
文件中
并使用字体
类相应地设置字体样式
Typeface style = Typeface.createFromAsset(asm, "fonts/Roboto-Bold.ttf");
view.setTypeFace(style);
注意,在这里,Roboto字体保存在另一个目录中,即资源中的字体
为了更好地实践,您可以创建一个单独的类来扩展所需的视图。除了包名之外,您还可以访问xml布局中创建的视图
public class BoldTextView extends TextView{
public BoldTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public BoldTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public BoldTextView(Context context) {
super(context);
init();
}
private void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"fonts/Roboto-Bold.ttf");
setTypeface(tf);
}
}
布局中对此类的引用:
com.your.packagename.BoldTextView