本示例介绍如何使用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.创建ActivityTranslucentActivity.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);

}

}

更多相关文章

  1. Android用户和用户组的定义
  2. Android 自定义 ViewGroup
  3. Android Java 自定义异常
  4. Android 自定义 spinner样式
  5. Android窗体自定义标题栏
  6. Android多媒体开发 Pro Android Media 第二章 创建自定义相机应
  7. Android——自定义带刻度的SeekBar单向拖动条 原创 2017年04月21
  8. Android 之 自定义标签 和 自定义组件
  9. Android进度条样式

随机推荐

  1. android 1.5 SDK 模拟器无法启动问题 “F
  2. Android(安卓)之Activity , Window和View
  3. Android(安卓)Studio上传SVN
  4. android FragmentActivity+FragmentTabHo
  5. 详解Android(安卓)视频播放时停止后台运
  6. Android(安卓)WiFi ADB
  7. react-native调用Android原生控件
  8. Android(安卓)4.4 Kitkat Phone工作流程
  9. Android支持Java8新特性
  10. android 报Unable to resolve target 'an