一手遮天 Android - view(文本类): TextView 字体相关

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

一手遮天 Android - view(文本类): TextView 字体相关

示例如下:

/view/text/TextViewDemo2.java

/**
 * TextView - 文本显示控件
 *
 * 演示 TextView 的字体相关的使用
 */

package com.webabcd.androiddemo.view.text;

import android.annotation.SuppressLint;
import android.graphics.Typeface;
import androidx.core.graphics.TypefaceCompat;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextPaint;
import android.widget.TextView;

import com.webabcd.androiddemo.R;

public class TextViewDemo2 extends AppCompatActivity {

    private TextView _textView6;
    private TextView _textView8;
    private TextView _textView9;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_text_textviewdemo2);

        _textView6 = (TextView) findViewById(R.id.textView6);
        _textView8 = (TextView) findViewById(R.id.textView8);
        _textView9 = (TextView) findViewById(R.id.textView9);

        sample();
    }

    private void sample() {
        // 指定自定义字体:方式一
        // 将字体文件保存到 assets 文件夹(例:assets/fonts/myfont.ttf),然后通过如下方式指定字体
        // Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/myfont.ttf");
        // _textView6.setTypeface(typeface);

        // 指定自定义字体:方式二(推荐方式,因为这种方式也可以在 xml 中使用)
        // 将字体文件保存到类似这样的路径 res/font/myfont.ttf,然后通过如下方式指定字体
        @SuppressLint("RestrictedApi")
        Typeface typeface = TypefaceCompat.createFromResourcesFontFile(this, this.getResources(), R.font.myfont, "", Typeface.NORMAL);
        _textView6.setTypeface(typeface);

        // 指定字体样式(Typeface.DEFAULT 相当于 xml 中的 typeface="normal";Typeface.NORMAL 相当于 xml 中的 textStyle="normal")
        _textView8.setTypeface(Typeface.DEFAULT, Typeface.BOLD_ITALIC);

        // 指定字体样式的高级用法(如果你的字体不支持粗体斜体之类的话,就可以尝试下面这样做)
        TextPaint tp  = _textView9.getPaint();
        tp.setFakeBoldText(true); // 粗体
        tp.setTextSkewX(-0.5f); // 斜体,float 类型,负数表示右斜,整数表示左斜
        tp.setUnderlineText(true); // 下划线
        tp.setStrikeThruText(true); // 删除线
    }
}

/layout/activity_view_text_textviewdemo2.xml

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

    <!--演示 TextView 的字体相关的使用-->

    <!--
        typeface - 字体
            normal - 默认字体(在我的设备测试,效果同 sans)
            sans - 非衬线字体(笔划粗细大致一样,没有额外的修饰)
            serif - 衬线字体(笔划的起始和结束部分有额外的修饰)
            monospace - 等宽字体(每一个字符都宽度相同)
    -->
    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:typeface="sans"
        android:textSize="24dp"
        android:text="kkkkkk0123456789" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:typeface="serif"
        android:textSize="24dp"
        android:text="0123456789kkkkkk" />

    <!--
        通过 typeface="monospace" 来保证每一个字符的宽度都是相同的
    -->
    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:typeface="monospace"
        android:textSize="24dp"
        android:text="kkkkkk0123456789" />
    <TextView
        android:id="@+id/textView4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:typeface="monospace"
        android:textSize="24dp"
        android:text="0123456789kkkkkk" />

    <!--
        使用自定义字体:把字体文件保存到如下位置 res/font/xxx.ttf
        然后通过 fontFamily="@font/xxx" 来使用此字体(需要用 api26 或以上的 sdk 编译)
    -->
    <TextView
        android:id="@+id/textView5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:fontFamily="@font/myfont"
        android:textSize="24dp"
        android:lines="1"
        android:text="你好 webabcd" />
    <!--
        在 java 中指定自定义字体
    -->
    <TextView
        android:id="@+id/textView6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:typeface="monospace"
        android:textSize="24dp"
        android:lines="1"
        android:text="你好 webabcd" />


    <!--
        textStyle - 文本样式
            normal, bold, italic
            注:支持“|”运算符
    -->
    <TextView
        android:id="@+id/textView7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textStyle="bold|italic"
        android:textSize="24dp"
        android:lines="1"
        android:text="你好 webabcd" />
    <TextView
        android:id="@+id/textView8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textSize="24dp"
        android:lines="1"
        android:text="你好 webabcd" />
    <TextView
        android:id="@+id/textView9"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textSize="24dp"
        android:lines="1"
        android:text="你好 webabcd" />

</LinearLayout>

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

posted @ 2021-05-31 12:19  webabcd  阅读(73)  评论(0编辑  收藏  举报