LoginSignup
44
41

More than 5 years have passed since last update.

Theme.Translucentを継承せずにActivityの背景を透過にする

Posted at

メモです。

たまたまActivityの背景を透過にしたく、さくっと調べてみたところほとんどが「Theme.Translucentを使おう、もう少し手を加えたければそれを継承したThemeを作ろう」ということだった。
確かにこれでできたのだけれど、そのActivityに限ってこれにはBaseに設定していたThemeが外れてしまい、各部品のStyleが変わってしまうという副作用がある。

Androidのソースを見ると

themes.xml
<!-- Default theme for translucent activities, that is windows that allow you
     to see through them to the windows behind.  This sets up the translucent
     flag and appropriate animations for your windows.  -->
<style name="Theme.Translucent">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <!-- Note that we use the base animation style here (that is no
         animations) because we really have no idea how this kind of
         activity will be used. -->
    <item name="android:windowAnimationStyle">@android:style/Animation</item>
</style>

となっていて、Activityの背景を透過にするには

    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowIsTranslucent">true</item>

の3つを指定するだけで足りる模様。

なので、最終的には自分の作ったThemeを継承して、上記の3を付け足した新しいThemeを該当Activityに設定する事で解決した。

44
41
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
44
41