【Android開発】TextViewのフォントサイズを変更する方法をステップ解説
この記事では、AndroidアプリでTextViewのフォントサイズ(文字サイズ)を変更する方法を、サンプルコードとともにわかりやすく解説します。
TextViewのサイズは、レイアウトXMLで android:textSize 属性を指定するだけで簡単に変更できます。単位には「sp」(scale-independent pixels)を使用するのが推奨されており、ユーザー側のフォントサイズ設定にも適切に対応できるようになります。
手順1:Android Studioで新規プロジェクトを作成する
まず、Android Studioを起動し、「File」→「New Project」を選択して、必要な項目をすべて入力し、新しいプロジェクトを作成しましょう。
手順2:res/layout/activity_main.xml にコードを追加する
以下のコードを res/layout/activity_main.xml に追加します。この例では、3つのTextViewに対してそれぞれ異なる textSize(64sp/36sp/24sp)とフォントファミリーを指定しています。
<?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:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Im Huge" android:fontFamily="sans-serif-smallcaps" android:layout_centerInParent="true" android:textSize="64sp" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/textView1" android:layout_marginBottom="10dp" android:text="Im small" android:layout_centerInParent="true" android:fontFamily="serif-monospace" android:textSize="36sp" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/textView2" android:layout_marginBottom="10dp" android:text="Im tiny" android:layout_centerInParent="true" android:fontFamily="serif-monospace" android:textSize="24sp" /> </RelativeLayout>
手順3:src/MainActivity.java にコードを追加する
次に、以下のコードを src/MainActivity.java に追加します。フォントサイズの変更はレイアウトXMLだけで完結するため、アクティビティ側では特別な処理は不要です。
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:androidManifest.xml にコードを追加する
最後に、以下のコードを androidManifest.xml に追加します。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.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」アイコンをクリックします。表示された選択肢から接続済みのモバイルデバイスを選ぶと、スマートフォンの画面に異なるサイズのテキストが表示され、textSize属性による変更が正しく反映されていることを確認できます。

-
Google Chromeでウェブサイトのフォントサイズを変更する方法
本や書籍が依然として親しまれている現代ですが、多くのインターネットユーザーはウェブサイトを「非常に長い本」のように捉えがちです。しかし実際には、ウェブサイトは常に進化し続けるインタラクティブなポータルであり、ユーザーの利便性に合わせてサイズや形を変えることができます。その一例がフォントサイズです。フォントサイズとは、ウェブサイトを開いたときに画面に表示される文字の大きさのことです。Google Chromeでは、ウェブサイトはあらかじめ設定された標準サイズで表示されますが、このサイズは自由に変更できます。文字が読みにくいと感じる場合は大きくしたり、一度にできるだけ多くの情報を表示したい場合は小
-
Windows 10で文字サイズを変更する方法を徹底解説!状況別の設定手順まとめ
ディスプレイの解像度やモニターのサイズによっては、Windows 10の文字が小さすぎたり大きすぎたりして、読みにくく感じることがあります。 この記事では、Windows 10の文字サイズを簡単に変更するさまざまな方法を、目的別にわかりやすくご紹介します。 なお、本記事の内容はYouTube動画でも解説していますので、あわせてご覧ください。 Windows 10で文字サイズを変更する基本手順 Windows 10のタイトルバー、メニュー、アイコンなどの文字サイズをまとめて変更するには、以下の手順を実行します。 デスクトップ上の任意の場所を右クリックし、「ディスプレイ設定」を左クリックします