Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Nunocky/4d7ccad571399b8aefce93b53a9dc54c to your computer and use it in GitHub Desktop.
Save Nunocky/4d7ccad571399b8aefce93b53a9dc54c to your computer and use it in GitHub Desktop.

Person

android studioのアプリケーション開発についての質問です。 一つのプロジェクトの中でアプリケーションとカスタムビューの開発を別モジュールで行おうとするとき、アプリケーションモジュールとカスタムビューモジュールで共通のThemeを使用することはできますか?

ChatGPT

はい、アプリケーションモジュールとカスタムビューモジュールで共通のThemeを使用することができます。そのためには、以下の手順に従って設定を行ってください。

  1. まず、プロジェクトレベルのbuild.gradleファイルに、共通のThemeを定義するリソースを含む新しいAndroidライブラリモジュールを追加します。
include ':app', ':customviewmodule', ':common'
  1. commonモジュールのbuild.gradleファイルに、以下のようにAndroidライブラリの設定を追加します。
apply plugin: 'com.android.library'

android {
    compileSdkVersion rootProject.compileSdkVersion

    defaultConfig {
        minSdkVersion rootProject.minSdkVersion
        targetSdkVersion rootProject.targetSdkVersion
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
    testImplementation 'junit:junit:4.13.1'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
  1. commonモジュールのsrc/main/res/valuesディレクトリに、共通のThemeを定義するthemes.xmlファイルを作成します。例えば、以下のように設定できます。
<resources>
    <style name="CommonTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryVariant">@color/colorPrimaryVariant</item>
        <item name="colorOnPrimary">@color/colorOnPrimary</item>
        <item name="colorSecondary">@color/colorSecondary</item>
        <item name="colorSecondaryVariant">@color/colorSecondaryVariant</item>
        <item name="colorOnSecondary">@color/colorOnSecondary</item>
        <item name="colorError">@color/colorError</item>
        <item name="colorOnError">@color/colorOnError</item>
    </style>
</resources>
  1. アプリケーションモジュールとカスタムビューモジュールのbuild.gradleファイルに、commonモジュールへの依存関係を追加します。
dependencies {
    ...
    implementation project(':common')
    ...
}

アプリケーションモジュールとカスタムビューモジュールのthemes.xmlファイルで、CommonThemeを継承して各モジュールのThemeを定義します。

例えば、アプリケーションモジュールのthemes.xmlファイルには以下のように記述します。

<resources>
    <style name="AppTheme" parent="CommonTheme">
        <!-- アプリケーションモジュール固有のカスタマイズをここに追加します。 -->
    </style>
</resources>

同様に、カスタムビューモジュールのthemes.xmlファイルには以下のように記述します。

<resources>
    <style name="CustomViewTheme" parent="CommonTheme">
        <!-- カスタムビューモジュール固有のカスタマイズをここに追加します。 -->
    </style>
</resources>

これで、アプリケーションモジュールとカスタムビューモジュールで共通のThemeを使用することができます。必要に応じて、各モジュールで固有のカスタマイズを行ってください。

最後に、アプリケーションモジュールのAndroidManifest.xmlファイルで、AppThemeを適用します。

<manifest ...>
    <application
        ...
        android:theme="@style/AppTheme"
        ...>
        ...
    </application>
</manifest>

カスタムビューモジュールの場合、個々のビューやアクティビティでCustomViewThemeを適用することができます。例えば、カスタムビューのコンストラクタで以下のように記述します。

public class MyCustomView extends View {
    public MyCustomView(Context context) {
        super(context, null, R.style.CustomViewTheme);
    }

    ...
}

これで、アプリケーションモジュールとカスタムビューモジュールで共通のThemeを使用し、各モジュールで必要なカスタマイズを行うことができます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment