在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。

更多相关文章

  1. Android中View自定义XML属性详解以及R.attr与R.styleable的区别
  2. Android开发实践:用PopupWindow实现自定义Dailog
  3. android自定义控件:可旋转View:可作为ImageView、ImageButton
  4. Android中自定义PopupWindow,动态弹窗。
  5. Android 自定义view完全解析--带你通透了解自定义view
  6. Android自定义实现圆形播放进度条
  7. Android中关于自定义Dialog的使用简介
  8. 深入解析_Android的自定义布局

随机推荐

  1. 【Android 电量优化】JobScheduler 源码
  2. Android NDK 知识系列(一)
  3. Android NDK编程入门
  4. 学习Android过程中的一些博客或工具收集
  5. Android's HTTP Clients -Android的http
  6. 最近整理的Android学习笔记
  7. 命令行下Android应用开发
  8. 【Android】魅族Flyme OS 3摄像头无法预
  9. Android编译详解之lunch命令
  10. android studio 导入eclipse项目 兼容性