1.自訂Activity顯示樣式
先在res/values下建colors.xml檔案,寫入:
<?xml version="1.0" encoding="utf-8"?><br /><resources><br /> <!-- 設定透明度為56%(9/16)左右 --><br /> <color name="transparent">#9000</color><br /></resources>
這個值設定了整個介面的透明度,為了看得見效果,現在設為透明度為56%(9/16)左右。
再在res/values/下建styles.xml,設定程式的風格
<?xml version="1.0" encoding="utf-8"?><br /><resources><br /><mce:style name="Transparent"><!--<br /> 設定背景 --><br /> <item name="android:windowBackground">@color/transparent</item><br /> <!-- 設定底層可見 --><br /> <item name="android:windowIsTranslucent">true</item><br /> <!-- 設定跳轉效果 --><br /> <item name="android:windowAnimationStyle">@+android:style/Animation.Translucent</item> </p><p>--></mce:style><style name="Transparent" mce_bogus="1"> 設定背景 --><br /> <item name="android:windowBackground">@color/transparent</item><br /> <!-- 設定底層可見 --><br /> <item name="android:windowIsTranslucent">true</item><br /> <!-- 設定跳轉效果 --><br /> <item name="android:windowAnimationStyle">@+android:style/Animation.Translucent</item><br /> </style><br /></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"?><br /><shape xmlns:android="http://schemas.android.com/apk/res/android"><br /> <solid android:color="#ffffff" /><br /> <stroke android:width="3dp" color="#000000" /><br /> <corners android:radius="3dp" /><br /> <padding android:left="3dp" android:top="3dp" android:right="3dp"<br /> android:bottom="3dp" /><br /></shape>
再在res/values/下建styles.xml,設定程式的風格
<?xml version="1.0" encoding="utf-8"?><br /><resources><br /> <!-- 設定樣式 --><br /><mce:style name="Theme.MyActivity" parent="android:style/Theme.Dialog" mce_bogus="1" mce_bogus="1"><!--<br /><item name="android:windowBackground">@drawable/bgconfig</item></p><p>--></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><br /></style><br /></resources><br />
註:mce部分為發帖是自動產生的,實際不需要。
最後一步,把這個styles.xml用在相應的Activity上。即在AndroidManifest.xml中的任意<activity>標籤中添加
android:theme = "@style/transparent"
如果想設定所有的activity都使用這個風格,可以把這句標籤語句添加在<application>中。
3.設定視窗大小和位置
WindowManager m = getWindowManager();<br /> Display d = m.getDefaultDisplay(); //為擷取螢幕寬、高 </p><p> LayoutParams p = getWindow().getAttributes(); //擷取對話方塊當前的參數值<br /> p.height = (int) (d.getHeight() * 1.0); //高度設定為螢幕的1.0<br /> p.width = (int) (d.getWidth() * 0.7); //寬度設定為螢幕的0.8<br /> p.alpha = 1.0f;//設定本身透明度<br /> p.dimAmount = 0.0f;//設定黑暗度</p><p> getWindow().setAttributes(p); //設定生效<br /> getWindow().setGravity(Gravity.RIGHT); //設定靠右對齊