這篇文章給大家分享的是有關Android如何設置透明度、黑暗度的內容。小編覺得挺實用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。
設置透明效果 大概有三種
1、用android系統的透明效果
Java代碼
android:background="@android:color/transparent"
例如 設置按鈕
Java代碼
<Button android:background="@android:color/transparent" android:text="@+id/Button01" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" />
2、用ARGB來控制
Java代碼
//半透明 <Button android:background="#e0000000" /> //透明 <Button android:background="#00000000" />
相關教程:Java視頻教程
3、設置alpha
Java代碼
View v = findViewById(R.id.content);//找到你要設透明背景的layout 的id v.getBackground().setAlpha(100);//0~255透明度值
android 窗體透明的,黑暗度等的設置技巧
設置透明度(這是窗體本身的透明度,非背景)
WindowManager.LayoutParams lp=getWindow().getAttributes(); lp.alpha=0.3f; getWindow().setAttributes(lp);
alpha在0.0f到1.0f之間。1.0完全不透明,0.0f完全透明
設置黑暗度
WindowManager.LayoutParams lp=getWindow().getAttributes(); lp.dimAmount=0.5f; getWindow().setAttributes(lp); getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
dimAmount在0.0f和1.0f之間,0.0f完全不暗,1.0f全暗
Activity的透明、半透明效果的設置transparent
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> //注意: //color.xml的#5000000前兩位是透明的效果參數從00--99(透明--不怎么透明), //后6位是顏色的設置
manifest.xml
<activity android:name=".TransparentActivity" android:theme="@style/Transparent"> </activity>
java代碼
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTheme(R.style.Transparent); setContentView(R.layout.transparent); }
感謝各位的閱讀!關于Android如何設置透明度、黑暗度就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。