本示例介绍如何使用Android系统样式和自定义样式创建半透明界面。
1. 定义清单文件(AndroidManifest.xml)<?xmlversion ="1.0"encoding="utf-8"?>
package="my.andriod.test"
android:versionCode="1"
android:versionName="1.0">
android:label="@string/app_name"
android:theme="@style/Theme.Translucent">
2. 定义字符串资源(strings.xml)<?xmlversion ="1.0"encoding="utf-8"?>
Hello World, TranslucentActivity!
TranslucentActivity
App/Activity/Translucent
Example of how you can make an
activity have a translucent background, compositingover
whatever is behind it.
3. 定义自定义样式(values/styles.xml)<?xmlversion ="1.0"encoding="utf-8"?>
@drawable/translucent_background
true
#fff
4. 定义半透明颜色(colors.xml)<?xmlversion ="1.0"encoding="utf-8"?>
#e0000000
5. 定义布局文件(translucent_background.xml)<?xmlversion ="1.0"encoding="utf-8"?>
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)packagemy.andriod.test;
importandroid.app.Activity;
importandroid.os.Bundle;
publicclassTranslucentActivity extendsActivity {
/** Activity首次创建时,调用这个方法*/
@Override
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//填充布局
setContentView(R.layout.translucent_background);
}
}