Activity transparent and translucent effect setting transparent of two implementation methods

  • 2020-05-09 19:16:54
  • OfStack

Method 1:
Create styles.xml under res/values:


<?xml version= " 1.0 "  encoding= " utf-8 " ?>  
<style name="translucent">
<item name="android:windowBackground">@color/translucent_background</item>
<item name="android:windowIsTranslucent">true</item>
</style> 

Under this folder create the file colors.xml

<?xml version= " 1.0 "  encoding= " UTF-8 " ?>  
<RESOURCES>  
<color name="translucent_background">#60000000</color>  
</RESOURCES>  

With this write setting, you have to tell Activity to use this write setting.
AndroidManifest. xml find the activity to pop up, add theme:
android: @ style/translucent theme = ""
Well, yes, it is. But here's the problem. button in layout is opaque. What if you could make them transparent or translucent, too? You have to set the window properties.

   Window window=getWindow();
         WindowManager.LayoutParams wl = window.getAttributes();
         wl.flags=WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
         wl.alpha=0.6f;     This is how you can set the transparency of these pieces on the window. 0 . 0 is completely transparent. 1 . 0 is opaque. 
         window.setAttributes(wl);
  

Method 2:
Today, try to make activity translucent effect, after making it found to be complicated! Very simple a few words can be achieved, not to say, paste the code!
res/values/styles.xml

<resources>  
  <style name="Transparent   
">  
    <item name="android:windowBackground">@color/transparent_background</item>  
    <item name="android:windowNoTitle">true</item>  
    <item name="android:windowIsTranslucent">true</item>     
    <item name="android:windowAnimationStyle">@+android:style/Animation.Translucent</item>  
  </style>  
</resources>  

res/values/color.xml

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
  <color name="transparent_background">#50000000</color>  
</resources>  

Note: color.xml #5000000 the first two are transparent effect parameters from 00- 99 (transparent - not so transparent), the last six are color Settings
manifest.xml

<activity android:name=".TransparentActivity" android:theme="@style/Transparent">  
</activity>  

java code

public void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);   
        setTheme(R.style.Transparent);    
        setContentView(R.layout.transparent);   
}  


Related articles: