下面是自定義Activity半透明的效果例子:
res/values/styles.xml
<resources>
<stylename="Transparent ">
<itemname="android:windowBackground">@color/transparent_background</item>
<itemname="android:windowNoTitle">true</item>
<itemname="android:windowIsTranslucent">true</item>
<itemname="android:windowAnimationStyle">@+android:style/Animation.Translucent</item>
</style>
</resources>java
res/values/color.xml
<?xmlversion="1.0"encoding="utf-8"?>
<resources>
<colorname="transparent_background">#50000000</color>
</resources>
注意:color.xml的#5000000前兩位是透明的效果參數從00 到 ff(透明--不麼透明),後6位是顏色的設置manifest.xml
<activityandroid:name=".TransparentActivity"
android:theme="@style/Transparent"/>
java代碼
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.Transparent);
setContentView(R.layout.transparent);
}android
下面是利用系統主題實現Activity半透明的效果例子:
Android爲透明效果提供了內置的主題:Theme(android:style/Theme.Translucent),若是想實現透明效果,只要爲Activity設置該Theme即可。若是想實現半透明效果,則只須要繼承android:style/Theme.Translucent,並重寫即可。繼承android:style/Theme.Translucent並重寫:
<?xmlversion=」1.0″ encoding=」utf-8″?>
<resources>
<stylename=」Theme.Translucent」 parent=」android:style/Theme.Translucent」>
<itemname=」android:windowBackground」>@color/translucent_background</item>
<itemname=」android:colorForeground」>#fff</item>
</style>
</resources>
AndroidMainfest.xml中使用該主題:
<activityandroid:name=」.Translucent」 android:label=」@string/app_name」
android:theme=」@style/Theme.Translucent」>
<intent-filter>
<actionandroid:name=」android.intent.action.MAIN」 />
<categoryandroid:name=」android.intent.category.LAUNCHER」 />
</intent-filter>
</activity>
下面是實現View半透明的效果例子:
Button或者ImageButton的背景設爲透明或者半透明
半透明:<Button android:background="#e0000000" ... />
透明: <Button android:background="#00000000" ... />
顏色和不透明度 (alpha) 值以十六進制表示法表示。任何一種顏色的值範圍都是 0 到 255(00 到 ff)
。對於 alpha,00 表示徹底透明,ff 表示徹底不透明。表達式順序是「aabbggrr」,其中「aa=alpha」
(00 到 ff);「bb=blue」(00 到 ff);「gg=green」(00 到 ff);「rr=red」(00 到 ff)。例如
,若是您但願對某疊加層應用不透明度爲 50% 的藍色,則應指定如下值:7fff0000
設置背景圖片透明度(超簡單)
Java代碼
View v = findViewById(R.id.content);//找到你要設透明背景的layout 的id
v.getBackground().setAlpha(100);//0~255透明度值app