Android 編程下實現 Activity 的透明效果

來源:互聯網
上載者:User

標籤:android   style   blog   io   ar   color   使用   sp   strong   

實現方式一(使用系統透明樣式)

通過配置 Activity 的樣式來實現,在 AndroidManifest.xml 找到要實現透明效果的 Activity,在 Activity 的配置中添加如下的代碼設定該 Activity 為透明樣式,但這種實現方式只能實現純透明的樣式,無法調整透明度,所以這種實現方式有一定的局限性,但這種方式實現簡單。

android:theme="@android:style/Theme.Translucent"
 
    <activity        android:name="cn.sunzn.transact.MainActivity"        android:label="@string/app_name"        android:theme="@android:style/Theme.Translucent" >        <intent-filter>            <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />        </intent-filter>    </activity>
 

實現方式二(使用自訂透明樣式)

這種方式的實現同樣要配置 Activity 的樣式,只不過這裡的樣式是我們自訂的。具體實現過程如下:

1 在 res/values/color.xml 檔案下加入一個透明顏色值,這裡的 color 參數,是兩位元一個單位,前兩位元是透明度,後面每兩位一對是16進位顏色數字,樣本中為白色。

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

2 在 res/values/styles.xml 檔案中加入一個自訂樣式,代碼如下。

 
<!-- item name="android:windowBackground"         設定背景透明度及其顏色值 --><!-- item name="android:windowIsTranslucent"      設定當前Activity是否透明--><!-- item name="android:windowAnimationStyle"     設定當前Activity進出方式--><style name="translucent">    <item name="android:windowBackground">@color/translucent_background</item>    <item name="android:windowIsTranslucent">true</item>    <item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item></style>
 

3 在 AndroidManifest.xml 找到要實現透明的 Activity,在想要實現透明的 Activity 中配置其屬性,代碼如下;也可在該 Activity 的 onCreat() 方法中調用 setTheme(R.style.translucent) 來實現。

 
<activity    android:name="cn.sunzn.transact.MainActivity"    android:label="@string/app_name"    android:theme="@style/translucent" >    <intent-filter>        <action android:name="android.intent.action.MAIN" />        <category android:name="android.intent.category.LAUNCHER" />    </intent-filter></activity>

Android 編程下實現 Activity 的透明效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.