Android TextView同时设置粗体和斜体

Android TextView同时设置粗体和斜体

效果图

Android TextView同时设置粗体和斜体_第1张图片

问题

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




    

    

    

    




你可能感兴趣的:(Android基础,粗体,斜体,textview)