🗂
[Android][Kotlin]背景透過のAndroidアプリを作る
themes.xmlのstyleタグの中に下記2行を追加するだけ。
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
超簡単だけど、これじゃ身も蓋もないから...。
↓参考コードはこちら
手順
- Android Studioでプロジェクト生成
- 透過設定
- 動かす。
1.Android Studioでプロジェクト生成
ま、適当に。
2.透過設定
themes.xml(values配下とnight配下に2つあるけど両方とも)に下記コードを追加
\<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.BackgroundTransparentSample" parent="Theme.Material3.DayNight.NoActionBar">
+ <item name="android:windowIsTranslucent">true</item>
+ <item name="android:windowBackground">@android:color/transparent</item>
</style>
<style name="Theme.BackgroundTransparentSample" parent="Base.Theme.BackgroundTransparentSample" />
\</resources>
3. 動かす。
アプリが起動。赤丸部分が表示されているのが分かる。
おまけ
Activityに色を少し付けてみる。
- activity_main.xmlにbackgroundを設定
\<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
+ android:background="#66ffff00"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
- 動かす。
背景に色がついた!!
結構簡単だった。
Discussion