android-为活动中的所有textViews设置字体?

是否可以为活动中的所有TextView设置字体? 我可以使用以下命令为单个textView设置字体:

TextView tv=(TextView)findViewById(R.id.textView1);

Typeface face=Typeface.createFromAsset(getAssets(), "font.ttf");

tv.setTypeface(face);

但是我想一次更改所有textViews,而不是为每个textView手动设置它,任何信息都将不胜感激!

7个解决方案

90 votes

解决方案1 ::只需通过将父视图作为参数传递来调用这些方法。

private void overrideFonts(final Context context, final View v) {

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);

}

} else if (v instanceof TextView ) {

((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "font.ttf"));

}

} catch (Exception e) {

}

}

解决方案2 ::您可以使用自定义字体将TextView类子类化,然后使用它代替textview。

public class MyTextView extends TextView {

public MyTextView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

init();

}

public MyTextView(Context context, AttributeSet attrs) {

super(context, attrs);

init();

}

public MyTextView(Context context) {

super(context);

init();

}

private void init() {

if (!isInEditMode()) {

Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font.ttf");

setTypeface(tf);

}

}

}

Shankar Agarwal answered 2020-01-17T18:18:47Z

8 votes

我个人收藏中的一个:

private void setFontForContainer(ViewGroup contentLayout) {

for (int i=0; i < contentLayout.getChildCount(); i++) {

View view = contentLayout.getChildAt(i);

if (view instanceof TextView)

((TextView)view).setTypeface(yourFont);

else if (view instanceof ViewGroup)

setFontForContainer((ViewGroup) view);

}

}

Red Hot Chili Coder answered 2020-01-17T18:19:07Z

3 votes

如果您正在寻找更通用的编程解决方案,我创建了一个静态类,该类可用于设置整个视图的字体(活动UI)。 请注意,我正在使用Mono(C#),但是您可以使用Java轻松实现它。

您可以向此类传递要自定义的布局或特定视图。 如果您想提高效率,可以使用Singleton模式实现它。

public static class AndroidTypefaceUtility

{

static AndroidTypefaceUtility()

{

}

//Refer to the code block beneath this one, to see how to create a typeface.

public static void SetTypefaceOfView(View view, Typeface customTypeface)

{

if (customTypeface != null && view != null)

{

try

{

if (view is TextView)

(view as TextView).Typeface = customTypeface;

else if (view is Button)

(view as Button).Typeface = customTypeface;

else if (view is EditText)

(view as EditText).Typeface = customTypeface;

else if (view is ViewGroup)

SetTypefaceOfViewGroup((view as ViewGroup), customTypeface);

else

Console.Error.WriteLine("AndroidTypefaceUtility: {0} is type of {1} and does not have a typeface property", view.Id, typeof(View));

}

catch (Exception ex)

{

Console.Error.WriteLine("AndroidTypefaceUtility threw:\n{0}\n{1}", ex.GetType(), ex.StackTrace);

throw ex;

}

}

else

{

Console.Error.WriteLine("AndroidTypefaceUtility: customTypeface / view parameter should not be null");

}

}

public static void SetTypefaceOfViewGroup(ViewGroup layout, Typeface customTypeface)

{

if (customTypeface != null && layout != null)

{

for (int i = 0; i < layout.ChildCount; i++)

{

SetTypefaceOfView(layout.GetChildAt(i), customTypeface);

}

}

else

{

Console.Error.WriteLine("AndroidTypefaceUtility: customTypeface / layout parameter should not be null");

}

}

}

在您的活动中,您将需要创建一个Typeface对象。 我使用放在我的Resources / Assets /目录中的.ttf文件在OnCreate()中创建我的。 确保该文件在其属性中被标记为Android资产。

protected override void OnCreate(Bundle bundle)

{

...

LinearLayout rootLayout = (LinearLayout)FindViewById(Resource.Id.signInView_LinearLayout);

Typeface allerTypeface = Typeface.CreateFromAsset(base.Assets,"Aller_Rg.ttf");

AndroidTypefaceUtility.SetTypefaceOfViewGroup(rootLayout, allerTypeface);

}

JCKortlang answered 2020-01-17T18:19:37Z

1 votes

扩展Agarwal的答案...您可以通过切换TextView的样式来设置常规,粗体,斜体等。

import android.content.Context;

import android.graphics.Typeface;

import android.util.AttributeSet;

import android.widget.TextView;

public class TextViewAsap extends TextView {

public TextViewAsap(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

init();

}

public TextViewAsap(Context context, AttributeSet attrs) {

super(context, attrs);

init();

}

public TextViewAsap(Context context) {

super(context);

init();

}

private void init() {

if (!isInEditMode()) {

Typeface tf = Typeface.DEFAULT;

switch (getTypeface().getStyle()) {

case Typeface.BOLD:

tf = Typeface.createFromAsset(getContext().getAssets(), "Fonts/Asap-Bold.ttf");

break;

case Typeface.ITALIC:

tf = Typeface.createFromAsset(getContext().getAssets(), "Fonts/Asap-Italic.ttf");

break;

case Typeface.BOLD_ITALIC:

tf = Typeface.createFromAsset(getContext().getAssets(), "Fonts/Asap-Italic.ttf");

break;

default:

tf = Typeface.createFromAsset(getContext().getAssets(), "Fonts/Asap-Regular.ttf");

break;

}

setTypeface(tf);

}

}

}

您可以这样创建您的Assets文件夹:

您的Assets文件夹应如下所示:

最后,您在xml中的TextView应该是TextViewAsap类型的视图。 现在它可以使用您编码的任何样式...

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Example Text"

android:textStyle="bold"/>

karenms answered 2020-01-17T18:20:10Z

1 votes

最佳答案

1.为一个textView设置自定义字体

Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "Fonts/FontName.ttf");

textView.setTypeface (typeface);

2.为所有textViews设置自定义字体

创建如下的Java类

public class CustomFont extends android.support.v7.widget.AppCompatTextView {

public CustomFont(Context context) {

super(context);

init();

}

public CustomFont(Context context, AttributeSet attrs) {

super(context, attrs);

init();

}

public CustomFont(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

init();

}

private void init() {

Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/FontName.ttf");

setTypeface(tf);

}

}

并在您的xml页面中

...

/>

=>

android:id="@+id/TextView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:text="KEEP IT SIMPLE"

android:textAlignment="center" />

Mahdi Hossaini answered 2020-01-17T18:20:52Z

0 votes

使用反射的“通用”方式示例:

**它提出了一个包含视图组子级方法setTextSize(int,float)的想法,但是您可以像对setTypeFace()的问题一样采用它

/**

* change text size of view group children for given class

* @param v - view group ( for example Layout/widget)

* @param clazz - class to override ( for example EditText, TextView )

* @param newSize - new font size

*/

public static void overrideTextSize(final View v, Class> clazz, float newSize) {

try {

if (v instanceof ViewGroup) {

ViewGroup vg = (ViewGroup) v;

for (int i = 0; i < vg.getChildCount(); i++) {

View child = vg.getChildAt(i);

overrideTextSize(child, clazz, newSize);

}

} else if (clazz.isAssignableFrom(v.getClass())) {

/** create array for params */

Class>[] paramTypes = new Class[2];

/** set param array */

paramTypes[0] = int.class; // unit

paramTypes[1] = float.class; // size

/** get method for given name and parameters list */

Method method = v.getClass().getMethod("setTextSize",paramTypes);

/** create array for arguments */

Object arglist[] = new Object[2];

/** set arguments array */

arglist[0] = TypedValue.COMPLEX_UNIT_SP;

arglist[1] = newSize;

/** invoke method with arguments */

method.invoke(v,arglist);

}

} catch (Exception e) {

e.printStackTrace();

}

}

警告:

使用反射应该非常小心。 反射课非常“例外”

例如,您应该检查是否存在注释,以防止出现各种不同的问题。 在方法SetTextSize()的情况下,最好检查批注android.view.RemotableViewMethod

ceph3us answered 2020-01-17T18:21:30Z

0 votes

您可以使用Calligraphy库,该库在此处可用:

Android书法库

Dariush Salami answered 2020-01-17T18:21:54Z

设置android textview字体,android-为活动中的所有textViews设置字体?相关推荐

  1. android:ellipsize=end 不起作用,Android应用开发Android TextView关于android:ellipsize=end的一个神奇bug解决方案...

    本文将带你了解Android应用开发Android TextView关于android:ellipsize=end的一个神奇bug解决方案,希望本文对大家学Android有所帮助. 疑惑 今天在开发过 ...

  2. android textview adapter,Android在FragmentPagerAdapter中的Fragment中设置TextView文本

    这个让我疯了.基本上,我想创建一个ViewPager并为其添加一些片段.然后,我想做的就是在Fragment的TextViews中设置一个值.我可以添加Fragments,并且它们会附加,但是当我在第 ...

  3. android textview 添加图片大小,Android_Android中使用TextView实现图文混排的方法,向TextView或EditText中添加图像比 - phpStudy...

    Android中使用TextView实现图文混排的方法 向TextView或EditText中添加图像比直接添加文本复杂一点点,需要用到标签. 只有一个src属性,该属性原则上应该指向一个图像地址或可 ...

  4. android textview svg,Android中使用SVG与WebFont矢量图标

    一.参考文献 二.html使用方法 1.下载字体 网上百度自己要使用的字体,一般下载的是ttf格式,需要4种(或5)格式,其他的格式可以通过在线工具基于ttf转换 web-fontmin(这个在线转换 ...

  5. android textview坐标,android – 获取TextView中文本的位置

    看看几个Paint方法: getTextBounds()和 measureText.我们可以使用它们来确定TextView中文本的偏移量.确定TextView中的偏移后,我们可以将其添加到TextVi ...

  6. android textview layoutparams,Android动态设置布局的LayoutParams属性总遇到造型异常

    Android动态设置布局的setLayoutParams()属性总遇到造型异常问题 android:id="@+id/introduce" android:layout_widt ...

  7. android textview import,android – textview中的镜像文本?

    我很确定使用4.0之前的TextView是不可能的. 镜像自定义TextView并不难: package your.pkg; import android.content.Context; impor ...

  8. android textview 白色,android – AutoCompleteTextview默认情况下,颜色设置为白色

    我在我的Android应用程序中使用了一个AutoCompleteTextView,它正常工作.我唯一遇到的问题是,默认情况下,建议的颜色为白色,我无法看到任何建议.所以当我开始打字时,列表会以白色条 ...

  9. android Textview 功能,Android:TextView的常用功能

    android:autoText如果设置,将自动执行输入值的拼写纠正.此处无效果,在显示输入法并输入的时候起作用 android:bufferType指定getText()方式取得的文本类别.选项ed ...

最新文章

  1. PHP 通过数组判断数组顺序输出是否是二叉排序树的后序遍历结果
  2. 机器学习(MACHINE LEARNING)MATLAB模拟排队论
  3. mysql命令行方式添加用户及设置权限
  4. Git提交项目到GitHub
  5. 异步/同步、阻塞/非阻塞的理解
  6. package-lock.json是做什么用的_做鱼缸用什么玻璃好?
  7. python整数反转_敲代码学Python:力扣简单算法之整数反转
  8. STM32工作笔记0059---独立看门狗实验
  9. C++ 入门5 ---- 类和动态内存分配(一)
  10. 牛逼程序猿的学习之路
  11. java 蜂鸣器_蜂鸣器驱动
  12. office2013 打开报错 无法访问您试图使用的功能所在的网络位置
  13. Jmeter分布式部署测试-----远程连接多台电脑做压力性能测试
  14. 使用apk来控制指纹(指纹型号迈瑞微 ECS120)
  15. 达梦数据库SQL学习
  16. 甜味芯片打印法了解一下:科学家用糖实现微电路曲面打印,连针尖发丝都可以 | Science...
  17. 个人微信api接口调用-微信群管理
  18. MLT 框架设计文档翻译
  19. python气象绘图速成_Python气象数据处理与绘图(11):矢量箭头图(风场,通量场)
  20. 每日一句英语(日更新)

热门文章

  1. 使用ngZorro中Upload自定义上传时XMLHttpRequest问题解决方法
  2. JVM学习(十四):垃圾收集器(万字介绍CMS、G1)
  3. 荣耀v8升级android 8,圈粉1亿!荣耀最后四款老机型不限量升级安卓8.0
  4. 36个创意广告海报欣赏 | Goodfav 杂志
  5. srio 门铃_如何使SkyBell HD门铃静音
  6. 还没抢到票?试下这个用 Python 写的最新抢票神器
  7. Python全栈笔记(一)
  8. UG/NX10二次开发学习视频目录整理(NXOPEN制图篇)
  9. Java迭代器(Iterator)的用法
  10. 长连接 、短连接、心跳机制与断线重连