正常情況下,我們開發的應用程序都會上占滿整個屏幕,那么怎么樣才能開發出自定義窗口大小的的程序呢?如下圖所示:
(轉自:http://www.ideasandroid.com/archives/339)
第一步,創建一個背景配置文件float_box.xml,放到res/drawable下:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="3dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> <solid android:color="#ffffff" /> <stroke android:width="3dp" color="#000000" /> </shape>
這里conrners是設定圓角的,radius是4個角統一的弧度半徑,padding是填充域,這里不是設置shape的填充域,而是設置里面content的填充域,solid是顏色,填充shape域的顏色,stroke是畫筆,類似於paint,這里設置線粗和顏色,這些都是api里的,自己可以去查查看!
第二步,定義一個對話框樣式,放到res/values/styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 定義一個樣式,繼承android系統的對話框樣式 android:style/Theme.Dialog -->
<style name="Theme.FloatActivity" parent="android:style/Theme.Dialog">
<!-- float_box為我們定義的窗口背景 -->
<item name="android:windowBackground">@drawable/float_box</item>
</style>
</resources>
這里定義一個style變量,名字是Theme.FloatActivity,繼承android的Dialog,引用float_box的shape作為背景。
第三步,創建一個視圖配置文件res/layout/float_activity.xml,一個ImageView和一個TextView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/ideasandroidlogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:src="@drawable/ideasandroid" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ideasandroidlogo"
android:text="@string/ideasandroidIntr"
android:textColor="@android:color/black" />
</RelativeLayout>
就是一個相對布局,一個ImageView一個TextView;
第四步創建我們的Activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 先去除應用程序標題欄 注意:一定要在setContentView之前
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 將我們定義的窗口設置為默認視圖
setContentView(R.layout.float_activity);
}
這里去除標題,之前的帖子里說過了,然后設置視圖為float_activity就行了!
最后一步,更改應用程序配置文件AndroidManifest.xml:
1 <activity android:name=".WindowActivityActivity" 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>
這樣就可以運行了我在寫的時候,出現那個@drawable spell錯誤,直接去window—>preferences—>General—>editors—>text editors—>spelling,把enable spell checking取消掉!網上這么說的!
