1.自定义Activity显示样式
先在res/values下建colors.xml文件,写入:
-
<?xml version="1.0" encoding="utf-8"?>
-
<resources>
-
-
<color name="transparent">#9000</color>
-
</resources>
这个值设定了整个界面的透明度,为了看得见效果,现在设为透明度为56%(9/16)左右。
再在res/values/下建styles.xml,设置程序的风格
-
<?xml version="1.0" encoding="utf-8"?>
-
<resources>
-
<mce:style name="Transparent">
-
-
<item name="android:windowBackground">@color/transparent</item>
-
-
<item name="android:windowIsTranslucent">true</item>
-
-
<item name="android:windowAnimationStyle">@+android:style/Animation.Translucent</item>
-
-
--></mce:style><style name="Transparent" mce_bogus="1"> 设置背景 -->
-
<item name="android:windowBackground">@color/transparent</item>
-
-
<item name="android:windowIsTranslucent">true</item>
-
-
<item name="android:windowAnimationStyle">@+android:style/Animation.Translucent</item>
-
</style>
-
</resources>
注:mce部分为发帖是自动生成的,实际不需要。
最后一步,把这个styles.xml用在相应的Activity上。即在AndroidManifest.xml中的任意<activity>标签中添加
android:theme = "@style/transparent"
如果想设置所有的activity都使用这个风格,可以把这句标签语句添加在<application>中。
最后运行程序,哈哈,是不是发现整个界面都被蒙上一层半透明了。最后可以把背景色#9000换成#0000,运行程序后,就全透明了,看得见背景下的所有东西可以却都操作无效。呵呵....
2.将Activity以Dialog的形式显示并自定义样式
先在res/drawable下建bgconfig.xml文件,写入:
-
<?xml version="1.0" encoding="utf-8"?>
-
<shape xmlns:android="http://schemas.android.com/apk/res/android">
-
<solid android:color="#ffffff" />
-
<stroke android:width="3dp" color="#000000" />
-
<corners android:radius="3dp" />
-
<padding android:left="3dp" android:top="3dp" android:right="3dp"
-
android:bottom="3dp" />
-
</shape>
再在res/values/下建styles.xml,设置程序的风格
-
<?xml version="1.0" encoding="utf-8"?>
-
<resources>
-
-
<mce:style name="Theme.MyActivity" parent="android:style/Theme.Dialog" mce_bogus="1" mce_bogus="1">
-
-
-
</mce:style><style name="Theme.MyActivity" parent="android:style/Theme.Dialog" mce_bogus="1" mce_bogus="1" mce_bogus="1"> <item name="android:windowBackground">@drawable/bgconfig</item>
-
</style>
-
</resources>
注:mce部分为发帖是自动生成的,实际不需要。
最后一步,把这个styles.xml用在相应的Activity上。即在AndroidManifest.xml中的任意<activity>标签中添加
android:theme = "@style/transparent"
如果想设置所有的activity都使用这个风格,可以把这句标签语句添加在<application>中。
3.设置窗口大小和位置
-
WindowManager m = getWindowManager();
-
Display d = m.getDefaultDisplay();
-
-
LayoutParams p = getWindow().getAttributes();
-
p.height = (int) (d.getHeight() * 1.0);
-
p.width = (int) (d.getWidth() * 0.7);
-
p.alpha = 1.0f;
-
p.dimAmount = 0.0f;
-
-
getWindow().setAttributes(p);
-
getWindow().setGravity(Gravity.RIGHT);