Android 2.2 API demos -- Dialog
16lz
2021-01-23
在Android中有一种类似于HTML和CSS将样式和内容分离的机制。我们可以将内容定义在layout的XML中,将样式定义在style的XML中。通过HTML和CSS的实践证明,这种分离更有益于代码的重用和维护。
Custom Dialog示例
Android官方API Demo中的Custom Dialog就是一个简单的示例。
首先,将样式(style)定义在res/values/styles.xml中。
Xml代码
<style name="Theme.CustomDialog" parent="android:style/Theme.Dialog"> <item name="android:windowBackground">@drawable/filled_box</item> </style>
其次,将内容(layout)定义在res/layout/custom_dialog_activity.xml中。
Xml代码
<?xml version="1.0" encoding="utf-8"?> <!-- This screen consists of a single text field that displays some text. --> <TextView xmlns:android=http://schemas.android.com/apk/res/android android:id="@+id/text" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical|center_horizontal" android:text="@string/custom_dialog_activity_text"/>
第三,将样式应用到内容上,见AndroidManifest.xml。 将Activity的theme属性配置成想要的style。
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>
在官方的示例中,一个style被应用在一个activity上了,其实一个style还可以被应用在view或者整个application上。如果将一个style应用在activity或者application上,那么这个style就被称作是theme。
更多相关文章
- Android中View自定义XML属性详解以及R.attr与R.styleable的区别
- Android开发实践:用PopupWindow实现自定义Dailog
- android自定义控件:可旋转View:可作为ImageView、ImageButton
- Android中自定义PopupWindow,动态弹窗。
- Android 自定义view完全解析--带你通透了解自定义view
- Android自定义实现圆形播放进度条
- Android中关于自定义Dialog的使用简介
- 深入解析_Android的自定义布局