手机怎么换大字体

手机怎么换大字体,第1张

如果您使用的是华为手机,可以按照以下方法 *** 作。

1、放大或缩小字体

进入设置 >显示和亮度 >字体与显示大小,拖移滑块调整字体大小。

2、放大或缩小屏幕内容

显示大小可等比放大或缩小应用内显示的内容(如文字、图片等)。

进入设置 >显示和亮度 >字体与显示大小,拖移滑块调整显示大小。

解决方案

1)Android默认方法 #1

你可以通过ID查找到View,然后挨个为它们设置字体。在单个View的情况下,它看起来也没有那么可怕。

Typeface customFont = Typeface.createFromAsset(this.getAssets(), "fonts/YourCustomFont.ttf")

TextView view = (TextView) findViewById(R.id.activity_main_header)

view.setTypeface(customFont)

但是在很多TextView、Button等文本组件的情况下,我敢肯定你不会喜欢这个方法的。:D

2)Android默认方法 #2

你可以为每个文本组件创建一个子类,如TextView、Button等,然后在构造函数中加载自定义字体。

public class BrandTextView extends TextView {

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

super(context, attrs, defStyle)

}

public BrandTextView(Context context, AttributeSet attrs) {

super(context, attrs)

}

public BrandTextView(Context context) {

super(context)

}

public void setTypeface(Typeface tf, int style) {

if (style == Typeface.BOLD) {

super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/YourCustomFont_Bold.ttf"))

} else {

super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/YourCustomFont.ttf"))

}

}

}

然后只需要将标准的文本控件替换成你自定义的就可以了(例如BrandTextView替换TextView)。

<com.your.package.BrandTextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="View with custom font"/>

<com.your.package.BrandTextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textStyle="bold"

android:text="View with custom font and bold typeface"/>

还有,你甚至可以直接在XML中添加自定义的字体属性。要实现这个,你需要定义你自己的declare-styleable属性,然后在组件的构造函数中解析它们。

为了不占篇幅介绍这么基础的东西,这里有一篇不错的文章告诉你怎么自定义控件属性。

http://www.5itjob.com/

在大多数情况下,这个方法还不赖,并且有一些优点(例如,切换字体粗细等等,字体可以在组件xml文件的typeface属性中定义)。但是我认为这个实现方法还是太重量级了,并且依赖大量的模板代码,为了一个替换字体的简单任务,有点儿得不偿失。

3)我的解决方案

理想的解决方案是自定义主题,然后应用到全局或者某个Activity。

但不幸的是,Android的android:typeface属性只能用来设置系统内嵌的字体,而非用户自定义字体(例如assets文件中的字体)。这就是为什么我们无法避免在Java代码中加载并设置字体。

所以我决定创建一个帮助类,使得这个 *** 作尽可能的简单。使用方法:

FontHelper.applyFont(context, findViewById(R.id.activity_root), "fonts/YourCustomFont.ttf")

并且这行代码会用来加载所有的基于TextView的文本组件(TextView、Button、RadioButton、ToggleButton等等),而无需考虑界面的布局层级如何。

标准(左)与自定义(右)字体的用法。

Standard (left) and Custom (right) fonts usage.

这是怎么做到的?非常简单:

public static void applyFont(final Context context, final View root, final String fontName) {

try {

if (root instanceof ViewGroup) {

ViewGroup viewGroup = (ViewGroup) root

for (int i = 0i <viewGroup.getChildCount()i++)

applyFont(context, viewGroup.getChildAt(i), fontName)

} else if (root instanceof TextView)

((TextView) root).setTypeface(Typeface.createFromAsset(context.getAssets(), fontName))

} catch (Exception e) {

Log.e(TAG, String.format("Error occured when trying to apply %s font for %s view", fontName, root))

e.printStackTrace()

}

}

正如你所看到的,所需要做的仅仅是将基于TextView的文本组件从布局中遍历出来而已。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/11231584.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-14
下一篇 2023-05-14

发表评论

登录后才能评论

评论列表(0条)

保存