標籤:
App->Activity->Custom Dialog 例子使用Activity 來實現自訂對話方塊
類CustomDialogActivity本身無任何特別之處。關鍵的一點是其在AndroidManifest.xml中的定義:
<activity android:name=".app.CustomDialogActivity" android:label="@string/activity_custom_dialog" android:theme="@style/Theme.CustomDialog"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.SAMPLE_CODE" /> </intent-filter> </activity>
CustomDialogActivity使用了自訂的CustomDialog 風格(Theme)。
Android應用可以使用自訂的介面風格(Theme),Theme 為一組相關的Style定義,可以應用於某個Activity或是整個Application。使用Theme的一個好處是可以為整個應用定義統一的介面風格(統一的背景色,字型等)。
定義Theme 和定義Style一樣, 必須定義在/res/values子目錄下,根項目名為resources, Theme 和Style的區別在於Theme應用於Activity和Application而 Style應用於單個的View。 其定義方法是一致的。
Style 定義支援 Inheritance, 也就是在定義新風格時可以基於系統定義的風格或是之前定義的風格:
如Theme.CustomDialog 定義就是基於Android的Dialog風格(parent)而只修改的WindowsBackground屬性,使用了褐色背景。
<!-- A theme for a custom dialog appearance. Here we use an ugly custom frame. --> <style name="Theme.CustomDialog" parent="android:style/Theme.Dialog"> <item name="android:windowBackground">@drawable/filled_box</item> </style>
背景使用了shape,filled_box.xml如下:
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#f0600000"/> <stroke android:width="3dp" android:color="#ffff8080"/> <corners android:radius="3dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /></shape>
【起航計劃 005】2015 起航計劃 Android APIDemo的魔鬼步伐 04 App->Activity->Custom Dialog Dialog形式的Activity,Theme的使用,Shape的使用