android自定義Activity窗口大小(theme運用)


http://gundumw100.iteye.com/blog/906195

正常情況下,我們開發的應用程序都會上占滿整個屏幕,那么怎么樣才能開發出自定義窗口大小的的程序呢?如下圖所示: 

 

實現起來非常簡單。 
第一步,創建一個背景配置文件float_box.xml,放到res/drawable下,如下所示(如看不懂可查看本站:): 

Xml代碼   收藏代碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.     <!-- 
  3. ** Copyright 2010, Ideasandroid 
  4. -->  
  5. <shape xmlns:android="http://schemas.android.com/apk/res/android">  
  6.     <solid android:color="#ffffff" />  
  7.     <stroke android:width="3dp" color="#000000" />  
  8.     <corners android:radius="3dp" />  
  9.     <padding android:left="10dp" android:top="10dp" android:right="10dp"  
  10.         android:bottom="10dp" />  
  11. </shape>  


第二步,定義一個對話框樣式,放到res/values/styles.xml,如下所示: 

Xml代碼   收藏代碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.     <!-- Copyright (C) 2010 IdeasAndroid 
  3. -->  
  4. <resources>  
  5.     <!-- 定義一個樣式,繼承android系統的對話框樣式 android:style/Theme.Dialog-->  
  6.     <style name="Theme.FloatActivity" parent="android:style/Theme.Dialog">  
  7.         <!-- float_box為我們定義的窗口背景-->  
  8.         <item name="android:windowBackground">@drawable/float_box</item>  
  9.     </style>  
  10. </resources>  


第三步,創建一個視圖配置文件res/layout/float_activity.xml,一個ImageView和一個TextView,如下所示: 

Xml代碼   收藏代碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- Copyright (C) 2010 IdeasAndroid 
  3. -->  
  4. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  5.     android:layout_width="fill_parent"   
  6.     android:layout_height="fill_parent">   
  7.     <ImageView   
  8.     android:id="@+id/ideasandroidlogo"  
  9.     android:layout_width="wrap_content"   
  10.     android:layout_height="wrap_content"  
  11.     android:layout_alignParentTop="true"  
  12.     android:src="@drawable/ideasandroid"  
  13.     />  
  14.     <TextView  android:layout_width="wrap_content" android:text="@string/ideasandroidIntr"  
  15.     android:layout_height="wrap_content"  
  16.     android:layout_below="@id/ideasandroidlogo"  
  17.     android:textColor="@android:color/black"  
  18.     />  
  19. </RelativeLayout>  


第四步創建我們的Activity,如下所示: 

Java代碼   收藏代碼
  1. public class FloatActivityDemo extends Activity {  
  2.     @Override  
  3.     public void onCreate(Bundle savedInstanceState) {  
  4.         super.onCreate(savedInstanceState);  
  5.         //先去除應用程序標題欄  注意:一定要在setContentView之前  
  6.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  7.         //將我們定義的窗口設置為默認視圖  
  8.         setContentView(R.layout.float_activity);  
  9.    
  10.     }  
  11. }  


最后一步,更改應用程序配置文件AndroidManifest.xml,將我們剛才創建的樣式應用到我們的Activity上,如下所示: 

Xml代碼   收藏代碼
  1. <activity android:name=".FloatActivityDemo" android:theme="@style/Theme.FloatActivity">  
  2.             <intent-filter>  
  3.                 <action android:name="android.intent.action.MAIN" />  
  4.                 <category android:name="android.intent.category.LAUNCHER" />  
  5.             </intent-filter>  
  6.         </activity>  


自己試一試吧! 

Demo下載地址:http://www.ideasandroid.com/android/demo/FloatActivityDemo.rar


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM