Android中自定義Activity和Dialog的位置大小背景和透明度等

轉自: http://blog.csdn.net/jdsjlzx/article/details/17539583


1.自定義Activity顯示樣式

先在res/values下建colors.xml文件,寫入:

[xhtml] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <!-- 設置透明度爲56%(9/16)左右 -->  
  4.     <color name="transparent">#9000</color>       
  5. </resources>    
 

這個值設定了整個界面的透明度,爲了看得見效果,現在設爲透明度爲56%(9/16)左右。

再在res/values/下建styles.xml,設置程序的風格

 

[xhtml] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <resources>  
  3.     <mce:style name="Transparent"><!-- 
  4.  設置背景 -->     
  5.         <item name="android:windowBackground">@color/transparent</item>  
  6.         <!-- 設置底層可見 -->     
  7.         <item name="android:windowIsTranslucent">true</item>  
  8.         <!-- 設置跳轉效果 -->  
  9.         <item name="android:windowAnimationStyle">@+android:style/Animation.Translucent</item>     
  10.       
  11. --></mce:style><style name="Transparent" mce_bogus="1"> 設置背景 -->     
  12.         <item name="android:windowBackground">@color/transparent</item>  
  13.         <!-- 設置底層可見 -->     
  14.         <item name="android:windowIsTranslucent">true</item>  
  15.         <!-- 設置跳轉效果 -->  
  16.         <item name="android:windowAnimationStyle">@+android:style/Animation.Translucent</item>     
  17.     </style>   
  18. </resources>  
 

注:mce部分爲發帖是自動生成的,實際不需要。

最後一步,把這個styles.xml用在相應的Activity上。即在AndroidManifest.xml中的任意<activity>標籤中添加 
android:theme = "@style/transparent" 
如果想設置所有的activity都使用這個風格,可以把這句標籤語句添加在<application>中。 
最後運行程序,哈哈,是不是發現整個界面都被蒙上一層半透明瞭。最後可以把背景色#9000換成#0000,運行程序後,就全透明瞭,看得見背景下的所有東西可以卻都操作無效。呵呵.... 


2.將Activity以Dialog的形式顯示並自定義樣式

先在res/drawable下建bgconfig.xml文件,寫入:

[xhtml] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">    
  3.     <solid android:color="#ffffff" />    
  4.     <stroke android:width="3dp" color="#000000" />    
  5.     <corners android:radius="3dp" />    
  6.     <padding android:left="3dp" android:top="3dp" android:right="3dp"    
  7.         android:bottom="3dp" />  
  8. </shape>   
 

再在res/values/下建styles.xml,設置程序的風格

[xhtml] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <resources>  
  3.         <!-- 設置樣式 -->  
  4.     <mce:style name="Theme.MyActivity" parent="android:style/Theme.Dialog" mce_bogus="1" mce_bogus="1"><!-- 
  5.         <item name="android:windowBackground">@drawable/bgconfig</item> 
  6.      
  7. --></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>  
  8.     </style>  
  9. </resources>  

注:mce部分爲發帖是自動生成的,實際不需要。

最後一步,把這個styles.xml用在相應的Activity上。即在AndroidManifest.xml中的任意<activity>標籤中添加 
android:theme = "@style/transparent" 
如果想設置所有的activity都使用這個風格,可以把這句標籤語句添加在<application>中。

 

3.設置窗口大小和位置

[java] view plaincopy
  1. WindowManager m = getWindowManager();    
  2.        Display d = m.getDefaultDisplay();  //爲獲取屏幕寬、高    
  3.            
  4.        LayoutParams p = getWindow().getAttributes();  //獲取對話框當前的參數值    
  5.        p.height = (int) (d.getHeight() * 1.0);   //高度設置爲屏幕的1.0   
  6.        p.width = (int) (d.getWidth() * 0.7);    //寬度設置爲屏幕的0.8   
  7.        p.alpha = 1.0f;      //設置本身透明度  
  8.        p.dimAmount = 0.0f;      //設置黑暗度  
  9.            
  10.        getWindow().setAttributes(p);     //設置生效  
  11.        getWindow().setGravity(Gravity.RIGHT);       //設置靠右對齊

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章