Android中自定義Activity和Dialog的位置大小背景和透明度等
1.自定義Activity顯示樣式
先在res/values下建colors.xml文件,寫入:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <!-- 設置透明度為56%(9/16)左右 -->
- <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="https://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"><!--
- <item name="android:windowBackground">@drawable/bgconfig</item>
- --></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); //高度設置為屏幕的1.0
- p.width = (int) (d.getWidth() * 0.7); //寬度設置為屏幕的0.8
- p.alpha = 1.0f; //設置本身透明度
- p.dimAmount = 0.0f; //設置黑暗度
- getWindow().setAttributes(p); //設置生效
- getWindow().setGravity(Gravity.RIGHT); //設置靠右對齊
最後更新:2017-04-03 12:53:57
上一篇:
知道ip 查服務器對應的mac
下一篇:
Android之ExpandableListView下拉分組的實現
C語言中如何寫一個簡單可移植而又足夠隨機的隨機數生成器
貴陽大數據交易所開創國內多個“之最”
用5寸手機本撬動"雲時代"移動互聯網
jQuery中通過$.browser來判斷瀏覽器
在ASP.NET MVC中如何應用多個相同類型的ValidationAttribute?
Magento FAQ Magento常見問題處理辦法.
【短視頻SDK - 參數解析】對焦模式、裁剪模式、視頻質量、分辨率、視頻比例、幀率、關鍵幀間隔等參數解析
技海無涯:正則表達式相關的知識和技術(1)——表達式
WEB頁麵傳參有哪些方式
Flume sink=avro rpc connection error