如何為整個 Android 應用程式設定預設字體系列?


本示例演示如何為整個 Android 應用程式設定預設字體系列。

步驟 1 − 在 Android Studio 中建立一個新專案,轉到 File ⇒ New Project 並填寫所有必需的詳細資訊以建立一個新專案。

步驟 2 − 將以下程式碼新增到 res/layout/activity_main.xml。

<? xml version= "1.0" encoding= "utf-8" ?>
<RelativeLayout 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 :layout_margin= "16dp"
   tools :context= ".MainActivity" >
   <TextView
      android :layout_width= "match_parent"
      android :layout_height= "wrap_content"
      android :layout_centerInParent= "true"
      android :text= "@string/lorem"
      android :textSize= "18sp" />
</RelativeLayout>

步驟 3 − 將以下程式碼新增到 src/MainActivity.java

package app.tutorialspoint.com.sample ;
import android.support.v7.app.AppCompatActivity ;
import android.os.Bundle ;
public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate (Bundle savedInstanceState) {
      super .onCreate(savedInstanceState) ;
      setContentView(R.layout. activity_main ) ;
   }
}

步驟 4 − 將以下程式碼新增到 res/values/styles.xml

<resources>
   <!-- Base application theme. -->
   <style name= "AppBaseTheme" 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>
   </style>
   <style name= "AppTheme" parent= "AppBaseTheme" >
      <item name= "android:textViewStyle" > @style/RobotoTextViewStyle </item>
      <item name= "android:buttonStyle" > @style/RobotoButtonStyle </item>
   </style>
   <style name= "RobotoTextViewStyle" parent= "android:Widget.TextView" >
      <item name= "android:fontFamily" > sans-serif-light </item>
   </style>
   <style name= "RobotoButtonStyle" parent= "android:Widget.Holo.Button" >
      <item name= "android:fontFamily" > sans-serif-light </item>
   </style>
</resources>

步驟 5 − 將以下程式碼新增到 androidManifest.xml

<? xml version= "1.0" encoding= "utf-8" ?>
<manifest xmlns: android = "http://schemas.android.com/apk/res/android"
   package= "app.tutorialspoint.com.sample" >
   <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>
</manifest>

讓我們嘗試執行您的應用程式。我假設您已將真實的 Android 移動裝置與計算機相連線。要從 Android Studio 執行應用程式,請開啟專案的一個活動檔案,然後單擊工具欄中的 Run  圖示。選擇您的移動裝置作為選項,然後檢查將顯示預設螢幕的移動裝置 –


更新於: 30-Jul-2019

2 千+ 次瀏覽

啟動你的 職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.