Android
 Computer >> コンピューター >  >> プログラミング >> Android

Kotlinを使ってAndroidのボタンテキストに特定のフォントを設定する方法

この記事では、Kotlinを使用してAndroidアプリのボタンテキストに特定のフォントを適用する方法を、ステップごとにわかりやすく解説します。

手順1:新規プロジェクトの作成

Android Studioを開き、メニューから「File」→「New Project」を選択して、必要な情報を入力し、新しいプロジェクトを作成します。

手順2:レイアウトファイルの編集

res/layout/activity_main.xml に以下のコードを追加します。

activity_main.xml のコード例

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="50dp"
    android:text="Tutorials Point"
    android:textAlignment="center"
    android:textColor="@android:color/holo_green_dark"
    android:textSize="32sp"
    android:textStyle="bold" />
<Button
    android:id="@+id/btnChangeFont"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Click to change font of the Button" />
<Button
    android:id="@+id/btnChangeFont2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/btnChangeFont"
    android:layout_centerInParent="true"
    android:layout_marginTop="10dp"
    android:text="Click to change font of the Button" />
</RelativeLayout>

手順3:MainActivity.ktの編集

src/MainActivity.kt に以下のコードを追加します。このコードでは、ボタンがクリックされたときに setTypeface() メソッドを使って、それぞれ異なるフォント(MONOSPACE、SERIF)とスタイル(BOLD、BOLD_ITALIC)を適用しています。

import android.graphics.Typeface
import android.graphics.Typeface.BOLD_ITALIC
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
    lateinit var btnChangeFont: Button
    lateinit var btnChangeFont2: Button
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
        btnChangeFont = findViewById(R.id.btnChangeFont)
        btnChangeFont.setOnClickListener { btnChangeFont.setTypeface(Typeface.MONOSPACE, Typeface.BOLD)}
        btnChangeFont2 = findViewById(R.id.btnChangeFont2)
        btnChangeFont2.setOnClickListener { btnChangeFont2.setTypeface(Typeface.SERIF, BOLD_ITALIC) }
    }
}

手順4:AndroidManifest.xmlの確認

androidManifest.xml に以下のコードが含まれていることを確認してください。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.example.q11">
    <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)アイコンをクリックします。

表示される選択肢から接続したモバイルデバイスを選択すると、実機の画面にアプリが起動します。

ボタンをタップすると、1つ目のボタンには等幅フォント(MONOSPACE)+太字、2つ目のボタンにはセリフ体(SERIF)+太字斜体が適用され、テキストのフォントが変化することが確認できます。

  1. Androidでボタンのテキストにフォントを設定する方法を徹底解説

    はじめに この記事では、Androidアプリでボタンのテキストに特定のフォントを設定する方法を解説します。ButtonやTextViewにはsetTypeface()メソッドが用意されており、これを使うことでフォントファミリー(書体)だけでなく、太字や斜体といったスタイルも自由に組み合わせて指定できます。 本記事のサンプルでは、ボタンをタップしたタイミングでフォントを切り替える例を、ステップごとにわかりやすく紹介します。 Typefaceで指定できる主なフォントファミリー Typeface.DEFAULT … 標準のフォント(Sans Serif) Typeface.DEFAULT_BOLD

  2. Tkinterでテキストのフォントを設定する方法をわかりやすく解説

    Tkinterには、ウィジェットにさまざまな機能を持たせるための組み込みメソッドや関数が多数用意されています。Tkinterアプリケーションでは、font(フォントファミリー, フォントサイズ, スタイル)属性を使用することで、テキストウィジェットのフォントプロパティを自由にカスタマイズできます。このタプルは、Textウィジェットのコンストラクタ内で直接宣言することが可能です。サンプルコード以下の例では、カスタマイズしたフォントプロパティを持つテキストウィジェットを作成しています。# tkinterライブラリをインポート from tkinter import * from tkinter i