Android TextView同时设置粗体和斜体

Android TextView同时设置粗体和斜体

效果图

在这里插入图片描述

问题

TextView的粗体和斜体无法同时生效,要么显示斜体,要么显示粗体。
错误代码:


tv_test3.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
tv_test3.setTypeface(Typeface.SANS_SERIF, Typeface.ITALIC);

正确示例:

tv_test3.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD_ITALIC);

参考完整代码

Activity

import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView tv_test1;
    private TextView tv_test2;
    private TextView tv_test3;
    private TextView tv_test4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_test1 = findViewById(R.id.tv_test1);
        tv_test2 = findViewById(R.id.tv_test2);
        tv_test3 = findViewById(R.id.tv_test3);
        tv_test4 = findViewById(R.id.tv_test4);

        tv_test1.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
        tv_test2.setTypeface(Typeface.SANS_SERIF, Typeface.ITALIC);

        tv_test3.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
        tv_test3.setTypeface(Typeface.SANS_SERIF, Typeface.ITALIC);

        tv_test4.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD_ITALIC);
    }
}

layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_test1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="粗体"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/tv_test2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="斜体"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/tv_test3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="粗体+斜体"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/tv_test4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="粗体+斜体"
        android:textSize="20dp" />


</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值