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

ActionBarActivityでActionBarの背景色を変更する方法

この記事では、ActionBarActivityのActionBar(アクションバー)の背景色を変更する手順について解説します。テーマのスタイル定義を編集することで、アプリ全体のデザインに合わせたカスタマイズが可能になります。

ステップ1:新規プロジェクトの作成

Android Studioを起動し、「File」→「New Project」を選択して新しいプロジェクトを作成します。必要な項目をすべて入力してプロジェクトのセットアップを完了させてください。

ステップ2:レイアウトファイルの編集

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

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    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:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

ステップ3:MainActivity.javaの編集

src/MainActivity.javaに以下のコードを追加します。

package com.app.sample;
import androidx.appcompat.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の編集

Manifests/AndroidManifest.xmlに以下のコードを追加します。

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

ステップ5:styles.xmlで背景色を設定

res/values/styles.xmlに以下のコードを追加します。ここでActionBarの背景色(colorPrimary)とタイトル文字色を指定します。

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">#995544</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="ActionBar.Solid.Ribbit.TitleTextStyle"
        parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">#976325</item>
    </style>
</resources>

ポイント: colorPrimaryの値を変更することで、ActionBarの背景色を自由にカスタマイズできます。上記の例では「#995544」という茶系の色を指定しています。

アプリの実行

それでは、アプリケーションを実行してみましょう。実機のAndroidスマートフォンをパソコンに接続していることを前提としています。Android Studioからアプリを実行するには、プロジェクト内のいずれかのアクティビティファイルを開き、ツールバーの「Run」アイコンをクリックしてください。デバイス選択画面で接続したモバイル端末を選択すると、端末に以下のような画面が表示されます。

ActionBarActivityでActionBarの背景色を変更する方法

  1. 【iOS】メモアプリの背景色を変更する方法|ライト/ダーク切り替えと罫線・グリッド設定も解説

    iOSの標準「メモ」アプリを使っていると、背景が端末全体の表示設定に自動的に連動してしまう点に不便さを感じることがあります。ライトモードが有効なときは明るい背景に、ダークモードのときは暗い背景になり、個別にコントロールできないように見えます。しかし実は、iOSではメモごとの背景色を簡単に変更できます。この記事では、特定のメモだけ背景を変える方法、すべてのメモに一括で適用する方法、さらに罫線やグリッド背景を追加するカスタマイズ術まで、わかりやすく解説します。注意:メモアプリで異なる背景を設定するには、端末がiOS 13 / iPadOS 13以降である必要があります。古いバージョンをお使いの場合

  2. Instagramストーリーの背景色を変更する5つの方法を徹底解説

    Instagramは、SNS好きにとって魅力的なプラットフォームです。撮影した写真の披露から、一人旅の思い出のスナップ共有まで、あらゆる用途にぴったりの媒体となっています。独自の豊富な機能、洗練されたインターフェース、そして無限の可能性により、特に若い世代を中心に絶大な人気を誇っています。画像や動画の共有、ストーリーの作成、新しい人とのつながり、発信力の拡大など、さまざまな使い方ができる万能アプリです。2012年にFacebook(現Meta)に買収されて以来、世界中で10億人以上のアクティブユーザーを抱えるまでに成長しました。 本記事では、Instagramの人気機能である「Instagr