Android 設置字體樣式

Android 設置字體樣式

1.首先下載字體樣式 我是在這裏下載的字體樣式http://www.zitixiazai.org/
2.然後在下載好的字體樣式放到assets項目文件下

在這裏插入圖片描述

接下來就是代碼的使用了 直接貼圖

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="字體樣式"
        android:textSize="20dp"/>
    <TextView
        android:id="@+id/tv_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="字體樣式"
        android:textSize="20dp"/>
      	tv_1=findViewById(R.id.tv_1);
        Typeface typeface=Typeface.createFromAsset(getAssets(),"FZMingSTJW.TTF");
        tv_1.setTypeface(typeface);

在這裏插入圖片描述

如果設置全局字體樣式的話可以在res文件下創建font文件夾,然後把字體樣式複製粘貼到font文件中去

在這裏插入圖片描述
在這裏插入圖片描述
這裏

這裏備註一下:這裏的命名要小寫的 , 然後我們就可以在資源文件中引用

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        //引用這裏的屬性
        <item name="android:fontFamily">@font/fzmingstjw</item>
    </style>

之後再入口文件application中android:theme屬性使用@style/AppTheme或者更換就行了

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        //引用
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章