android自訂Activity視窗大小(theme運用)

來源:互聯網
上載者:User

標籤:idm   tools   int   enc   end   shape   box   art   idea   

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

android自訂Activity視窗大小(theme運用)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.