Android如何使用樣式建立半透明表單

來源:互聯網
上載者:User

本樣本介紹如何使用Android系統樣式和自訂樣式建立半透明介面。

1. 定義資訊清單檔(AndroidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

      package="my.andriod.test"

      android:versionCode="1"

      android:versionName="1.0">

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <!-- 使用自訂半透明主題樣式  android:theme="@style/Theme.Translucent" -->

        <activity android:name=".TranslucentActivity"

                  android:label="@string/app_name"              

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

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

        <!-- 使用Android系統半透明主題樣式

        <activity android:name=".TranslucentActivity"

                  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>

         -->

    </application>

    <uses-sdk android:minSdkVersion="9" />

</manifest>

2. 定義字串資源(strings.xml)

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string name="hello">Hello World, TranslucentActivity!</string>

    <string name="app_name">TranslucentActivity</string>

    <string name="activity_translucent">App/Activity/Translucent</string>

    <string name="translucent_background">Example of how you can make an

            activity have a translucent background, compositing over

            whatever is behind it.</string>

</resources>

3. 定義自訂樣式(values/styles.xml)

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <!-- 繼承Android系統的半透明主題樣式,並指定半透明背景的顏色、隱藏視窗標題和前景色彩  -->

    <style name="Theme.Translucent" parent="android:style/Theme.Translucent">

        <item name="android:windowBackground">@drawable/translucent_background</item>

        <item name="android:windowNoTitle">true</item>

        <item name="android:colorForeground">#fff</item>

    </style>

</resources>

4. 定義半透明顏色(colors.xml)

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <drawable name="translucent_background">#e0000000</drawable>

</resources>

5. 定義布局檔案(translucent_background.xml)

<?xml version="1.0" encoding="utf-8"?>

<!-- 在Activity中顯示文本 -->

<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text"

    android:layout_width="match_parent" android:layout_height="match_parent"

    android:gravity="center_vertical|center_horizontal"

    android:text="@string/translucent_background"/>

6. 建立Activity(TranslucentActivity.java)

package my.andriod.test;

 

import android.app.Activity;

import android.os.Bundle;

 

publicclass TranslucentActivity extends Activity {

    /** Activity首次建立時,調用這個方法 */

    @Override

    publicvoid onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        //填充布局

        setContentView(R.layout.translucent_background);

    }

}


摘自 FireOfStar的專欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.