1. 程式人生 > >android設定Activity背景色為透明的2種方法

android設定Activity背景色為透明的2種方法

android設定背景色為透明

方法一:

只要在配置檔案內activity屬性配置內加上

android:theme="@android:style/Theme.Translucent" 

就好了。

這樣就呼叫了android的透明樣式!

方法二:

先在res/values下建colors.xml檔案,寫入:
  
  <?xmlversion="1.0"encoding="UTF-8"?>
  
  <resources>
  
  <colorname="transparent">#9000</color>
  
  </resources>
  
  這個值設定了整個介面的透明度,為了看得見效果,現在設為透明度為56%(9/16)左右。
  
  再在res/values/下建styles.xml,設定程式的風格
  
  <?xmlversion="1.0"encoding="utf-8"?>
  
  <resources>
  
  <stylename="Transparent">
  
  <itemname="android:windowBackground">@color/transparent</item>
  
  <itemname="android:windowIsTranslucent">true</item>
  
  <itemname="android:windowAnimationStyle">@+android:style/Animation.Translucent</item>
  
  </style>
  
  </resources>
  
  最後一步,把這個styles.xml用在相應的Activity上。即在AndroidManifest.xml中的任意<activity>標籤中新增
  
  android:theme="@style/transparent"
  
  如果想設定所有的activity都使用這個風格,可以把這句標籤語句新增在<application>中。
  
  最後執行程式,哈哈,是不是發現整個介面都被蒙上一層半透明瞭。最後可以把背景色#9000換成#0000,執行程式後,就全透明瞭,看得見背景下的所有東西可以卻都操作無效。