Android 字体及样式设置

测试应用中有关字体设置的接口:

package com.gzgd.typefacetest;

import android.app.Activity;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;



public class MainActivity extends Activity {
    public static final String TAG ="Typeface";
    TextView tv;
    Button bu1;
    Button bu2;
    int typefacenum=0;
    int typestylenum=0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = findViewById(R.id.tv);
        bu1 = (Button) findViewById(R.id.button1);
        bu2 = (Button) findViewById(R.id.button2);

        bu2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch ((++typefacenum)%4) {
                    case 1:
                        tv.setTypeface(Typeface.DEFAULT);
                        break;
                    case 2:
                        tv.setTypeface(Typeface.SERIF);
                        break;
                    case 3:
                        tv.setTypeface(Typeface.SANS_SERIF);
                        break;
                    case 4:
                        tv.setTypeface(Typeface.DEFAULT_BOLD);
                        break;
                    case 0:
                        tv.setTypeface(Typeface.MONOSPACE);
                        break;
                    default:
                            tv.setTypeface(Typeface.MONOSPACE);


                }
            }
        });
        bu1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch ((++typestylenum)%3) {
                    case 1:
                        tv.setTypeface(tv.getTypeface(),Typeface.BOLD);
                        break;
                    case 2:
                        tv.setTypeface(tv.getTypeface(),Typeface.BOLD_ITALIC);
                        break;
                    case 3:
                        tv.setTypeface(tv.getTypeface(),Typeface.NORMAL);
                        break;
                    case 0:
                        tv.setTypeface(tv.getTypeface(),Typeface.ITALIC);
                        break;
                    default:
                        tv.setTypeface(tv.getTypeface(),Typeface.ITALIC);

                }
            }
        });
    }



}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:typeface="monospace"
        android:textStyle="italic"
        android:textSize="40dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="样式"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/tv"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="字体"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/tv"
        app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>

猜你喜欢

转载自blog.csdn.net/u011494885/article/details/82761620