Android textView中等加粗字体

本文介绍了一种在Android中实现中等加粗(Medium)字体的方法,通过调整TextPaint的描边宽度来达到效果,适用于需要在Android应用中匹配iOS设计稿中文字样式的情况。

     一次偶然的机会发现了一个东西,在Android里面默认的textStyle只有加粗和不加粗,但是很多UI的设计稿是以IOS做的会有Medium中等加粗这个概念,查了一遍资料没有好用的。不过有看到说

//获取当前控件的画笔
TextPaint paint = tv.getPaint();
//设置画笔的描边宽度值
paint.setStrokeWidth(2);
paint.setStyle(Paint.Style.FILL_AND_STROKE);

这样写的可以解决,我去试过不行。然后接着找资料,还是没找到,我只得回头来看这个是怎么回事

研究之后发现

paint.setStrokeWidth(2);     它的数值是  0-2(正常情况下) 0是普通字体  2是bold字体

所以我们开发中要使用中等字体  写0-2直接的数字就好。

不过不知道是我的错觉么,我最开始也是试过的发现不行,但是后面我试又可以了,不过我把参数写成  就可以

paint.setStrokeWidth(1.0f); float类型  

如果只是但个地方使用 

TextView tv3 = findViewById(R.id.tv3);
TextPaint paint = tv3.getPaint();
paint.setStrokeWidth(1.0f);
paint.setStyle(Paint.Style.FILL_AND_STROKE);

如果是多个地方要使用 ,可以这样写

public class MediumBoldTextView extends TextView {
    public MediumBoldTextView(Context context) {
        super(context);
    }

    public MediumBoldTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MediumBoldTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //获取当前控件的画笔
        TextPaint paint = getPaint();
        //设置画笔的描边宽度值
        paint.setStrokeWidth(1.0f);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        super.onDraw(canvas);
    }
}

这个范围值可以自己去控制

### 回答1: 要在Android TextView加粗字体,可以使用以下代码: 在XML布局文件中: ``` <TextView android:id="@+id/myTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:textStyle="bold" /> ``` 在Java代码中: ``` TextView myTextView = findViewById(R.id.myTextView); myTextView.setTypeface(null, Typeface.BOLD); ``` 这将使TextView中的文本加粗。 ### 回答2: 在Android开发中,经常需要对TextView字体进行样式设置,比如加粗。下面将介绍几种实现Android TextView字体加粗的方法。 方法一: 使用Android自带的android:textStyle属性,将其值设为“bold”,即可将TextView字体加粗。例如: ```xml <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:textStyle="bold" /> ``` 方法二: 同样可以使用Java代码设置TextView字体样式,即通过TextView的setTypeface()方法设置字体样式。其中,可以使用Typeface类提供的常量值来设置字体的样式。例如: ```java TextView textView = findViewById(R.id.text_view); textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)); ``` 方法三: 还可以通过在TextView的前面添加“B”标签来实现字体加粗。例如: ```xml <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="<b>Hello World!</b>" /> ``` 需要注意的是,在使用这种方法时,必须允许TextView解析HTML代码,即需要在Java代码中调用textView.setText(Html.fromHtml("..."))。例如: ```java TextView textView = findViewById(R.id.text_view); textView.setText(Html.fromHtml("<b>Hello World!</b>")); ``` 在上述三种方法中,方法一是最简单最常用的一种,只需要在xml文件中添加android:textStyle="bold"即可。而方法二和方法三可以根据需要进行选择,比如在需要动态设置字体样式时,可以使用方法二;而需要在TextView中显示HTML文本时,可以使用方法三。 ### 回答3: 在 Android 中,我们可以通过代码或 XML 的属性方式为 TextView 设置字体加粗效果。 一、代码方式: ```java // 获取 TextView 控件 TextView textView = findViewById(R.id.text_view); // 设置字体加粗 textView.setTypeface(null, Typeface.BOLD); ``` 在以上代码中,`setTypeface()` 方法用于设置字体,接收两个参数:第一个参数表示字体的类型,传入 `null` 表示不修改字体类型;第二个参数表示字体样式,传入 `Typeface.BOLD` 表示加粗字体。 二、属性方式: 在 XML 布局文件中,可以直接指定 TextView字体加粗属性: ```xml <TextView android:id="@+id/text_view" android:text="Hello World!" android:textStyle="bold" /> ``` 在以上代码中,`android:textStyle` 属性用于设置字体样式,传入 `bold` 表示加粗字体。 以上两种方式都可以实现 TextView字体加粗效果。最终展示的效果如下图所示: ![text_view_bold](https://user-images.githubusercontent.com/57215723/137758767-9f60dcc4-20cf-49b9-9116-9dd3e4ee4c90.png) 如果希望同时设置字体颜色、字体大小等其他属性,可以继续在代码或 XML 中指定相应属性即可。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值