android studioのアプリケーション開発についての質問です。 一つのプロジェクトの中でアプリケーションとカスタムビューの開発を別モジュールで行おうとするとき、アプリケーションモジュールとカスタムビューモジュールで共通のThemeを使用することはできますか?
はい、アプリケーションモジュールとカスタムビューモジュールで共通のThemeを使用することができます。そのためには、以下の手順に従って設定を行ってください。
- まず、プロジェクトレベルの
build.gradle
ファイルに、共通のThemeを定義するリソースを含む新しいAndroidライブラリモジュールを追加します。
include ':app', ':customviewmodule', ':common'
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'
}
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>
- アプリケーションモジュールとカスタムビューモジュールの
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を使用し、各モジュールで必要なカスタマイズを行うことができます。