将字体一次设置为所有布局…android
是否可以一次将字体设置为所有视图(包括列表视图),而不是对每个视图都这样做。谢谢您可以使用以下方法将字体设置为布局将字体一次设置为所有布局…android,android,typeface,Android,Typeface,是否可以一次将字体设置为所有视图(包括列表视图),而不是对每个视图都这样做。谢谢您可以使用以下方法将字体设置为布局 public void setFont(ViewGroup group, Typeface font) { int count = group.getChildCount(); View v; for (int i = 0; i < count; i++) { v = group.getChildAt(i
public void setFont(ViewGroup group, Typeface font) {
int count = group.getChildCount();
View v;
for (int i = 0; i < count; i++) {
v = group.getChildAt(i);
if (v instanceof TextView || v instanceof EditText || v instanceof Button) {
((TextView) v).setTypeface(font);
} else if (v instanceof ViewGroup)
setFont((ViewGroup) v, font);
}
}
public void setFont(视图组,字体){
int count=group.getChildCount();
观点五;
for(int i=0;i
您可以使用以下方法设置版面的字体
public void setFont(ViewGroup group, Typeface font) {
int count = group.getChildCount();
View v;
for (int i = 0; i < count; i++) {
v = group.getChildAt(i);
if (v instanceof TextView || v instanceof EditText || v instanceof Button) {
((TextView) v).setTypeface(font);
} else if (v instanceof ViewGroup)
setFont((ViewGroup) v, font);
}
}
public void setFont(视图组,字体){
int count=group.getChildCount();
观点五;
for(int i=0;i
使用以下代码:
public static void APPLY_FONT(final Context context, final View root,
final String fontName) {
try {
if (root instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) root;
for (int i = 0; i < viewGroup.getChildCount(); i++)
APPLY_FONT(context, viewGroup.getChildAt(i), fontName);
} else if (root instanceof TextView)
((TextView) root).setTypeface(Typeface.createFromAsset(
context.getAssets(), fontName));
} catch (Exception e) {
Log.e("ProjectName", String.format(
"Error occured when trying to apply %s font for %s view",
fontName, root));
e.printStackTrace();
}
}
public static void overrideFonts(final Context context, final View v, String fonts) {
try {
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
overrideFonts(context, child, fonts);
}
}else if (v instanceof EditText) {
((EditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}else if (v instanceof Button) {
((Button) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}else if (v instanceof TextView) {
((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}else if (v instanceof SwitchCompat) {
((SwitchCompat) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}else if (v instanceof AppCompatEditText) {
((AppCompatEditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}else if (v instanceof Spinner) {
((AppCompatEditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}else if (v instanceof AppCompatButton) {
((AppCompatEditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}
} catch (Exception e) {
e.printStackTrace();
}
}
publicstaticvoidapply\u字体(最终上下文、最终视图根、,
最终字符串(名称){
试一试{
if(视图组的根实例){
ViewGroup ViewGroup=(ViewGroup)根目录;
对于(int i=0;i
传递外部布局id和字体名称。使用以下代码:
public static void APPLY_FONT(final Context context, final View root,
final String fontName) {
try {
if (root instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) root;
for (int i = 0; i < viewGroup.getChildCount(); i++)
APPLY_FONT(context, viewGroup.getChildAt(i), fontName);
} else if (root instanceof TextView)
((TextView) root).setTypeface(Typeface.createFromAsset(
context.getAssets(), fontName));
} catch (Exception e) {
Log.e("ProjectName", String.format(
"Error occured when trying to apply %s font for %s view",
fontName, root));
e.printStackTrace();
}
}
public static void overrideFonts(final Context context, final View v, String fonts) {
try {
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
overrideFonts(context, child, fonts);
}
}else if (v instanceof EditText) {
((EditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}else if (v instanceof Button) {
((Button) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}else if (v instanceof TextView) {
((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}else if (v instanceof SwitchCompat) {
((SwitchCompat) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}else if (v instanceof AppCompatEditText) {
((AppCompatEditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}else if (v instanceof Spinner) {
((AppCompatEditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}else if (v instanceof AppCompatButton) {
((AppCompatEditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}
} catch (Exception e) {
e.printStackTrace();
}
}
publicstaticvoidapply\u字体(最终上下文、最终视图根、,
最终字符串(名称){
试一试{
if(视图组的根实例){
ViewGroup ViewGroup=(ViewGroup)根目录;
对于(int i=0;i
传递外部布局id和字体名称。每个人都可以使用此代码:
public static void APPLY_FONT(final Context context, final View root,
final String fontName) {
try {
if (root instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) root;
for (int i = 0; i < viewGroup.getChildCount(); i++)
APPLY_FONT(context, viewGroup.getChildAt(i), fontName);
} else if (root instanceof TextView)
((TextView) root).setTypeface(Typeface.createFromAsset(
context.getAssets(), fontName));
} catch (Exception e) {
Log.e("ProjectName", String.format(
"Error occured when trying to apply %s font for %s view",
fontName, root));
e.printStackTrace();
}
}
public static void overrideFonts(final Context context, final View v, String fonts) {
try {
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
overrideFonts(context, child, fonts);
}
}else if (v instanceof EditText) {
((EditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}else if (v instanceof Button) {
((Button) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}else if (v instanceof TextView) {
((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}else if (v instanceof SwitchCompat) {
((SwitchCompat) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}else if (v instanceof AppCompatEditText) {
((AppCompatEditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}else if (v instanceof Spinner) {
((AppCompatEditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}else if (v instanceof AppCompatButton) {
((AppCompatEditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}
} catch (Exception e) {
e.printStackTrace();
}
}
publicstaticvoidoverridefonts(最终上下文、最终视图v、字符串字体){
试一试{
if(视图组的v实例){
视图组vg=(视图组)v;
对于(int i=0;i
每个人都可以使用此代码:
public static void APPLY_FONT(final Context context, final View root,
final String fontName) {
try {
if (root instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) root;
for (int i = 0; i < viewGroup.getChildCount(); i++)
APPLY_FONT(context, viewGroup.getChildAt(i), fontName);
} else if (root instanceof TextView)
((TextView) root).setTypeface(Typeface.createFromAsset(
context.getAssets(), fontName));
} catch (Exception e) {
Log.e("ProjectName", String.format(
"Error occured when trying to apply %s font for %s view",
fontName, root));
e.printStackTrace();
}
}
public static void overrideFonts(final Context context, final View v, String fonts) {
try {
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
overrideFonts(context, child, fonts);
}
}else if (v instanceof EditText) {
((EditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}else if (v instanceof Button) {
((Button) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}else if (v instanceof TextView) {
((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}else if (v instanceof SwitchCompat) {
((SwitchCompat) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}else if (v instanceof AppCompatEditText) {
((AppCompatEditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}else if (v instanceof Spinner) {
((AppCompatEditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}else if (v instanceof AppCompatButton) {
((AppCompatEditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
}
} catch (Exception e) {
e.printStackTrace();
}
}
publicstaticvoidoverridefonts(最终上下文、最终视图v、字符串字体){
试一试{
if(视图组的v实例){
视图组vg=(视图组)v;
对于(int i=0;i
setTypeFace仅对TextView和EditText有意义。是的,这是正确的,但根据下面的答案,有一种方法可以检查每个视图,然后设置字体。但是为什么不呢