[ApiDemos] Activity CustomDialog

来源:互联网 发布:淘宝账号简介怎么写 编辑:程序博客网 时间:2024/05/29 08:22

这次要看的 Demo 是 CustomDialog,位于 app->CustomDialogActivity。这个 activity 比较特殊,是一个对话框形式呈现的。这是怎么实现的呢?一起来看一下:

CustomDialogActivity 里边的代码非常简单,只有一行加载布局文件的代码,而且布局文件也是只有一个 TextView 控件,貌似没有什么不一样的?其实奥妙不在这里,看一下 AndroidManifest 文件就知道了。

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

其实起作用的只有一行代码 android:theme=”@style/Theme.CustomDialog”,这行代码的意思就是给这个 activity 指定一个 CustomDialog 的主题,我们先不着急去看它内部的代码,先来了解下什么是theme?

什么是theme?

看看官方文档对theme怎么解释的:

android:theme

A reference to a style resource defining an overall theme for the activity. This automatically sets the activity’s context to use this theme (see setTheme(), and may also cause “starting” animations prior to the activity being launched (to better match what the activity actually looks like).
If this attribute is not set, the activity inherits the theme set for the application as a whole — from the element’s theme attribute. If that attribute is also not set, the default system theme is used. For more information, see the Styles and Themes developer guide.

由于笔者英文不是很好,就解释一下大概意思。设置theme属性后就会作用于整个 activity,也可以通过代码setTheme()设置,可能会造成比activity 的开始动画先启动。如果activity没设置这个属性,就是集成application的,如果application也没设置就使用系统默认主题。

接下来看下 Styles and Themes的描述:

A style is a collection of properties that specify the look and format for a View or window. A style can specify properties such as height, padding, font color, font size, background color, and much more. A style is defined in an XML resource that is separate from the XML that specifies the layout.

翻译过来大概意思就是一个 style 可以制定view或者 window 的属性即可,一个 style 能至指定 height、padding等各种属性,一个 style 资源文件也可以作用于单独的layout。

看了这些文档简单的总结一下:

1.Theme 是作用于整个应用或者 activity 的主题,使用 style文 件,此节点主要是作用于窗体
2.Style 作为一个资源文件使用,可以指定其引用它的上下文的样式,如高宽等;要注意的是它的作用范围,如果在 application 里引用就作用于整个 application,如果一个 view 引用就作用于整个 view。

下面我们就可以看一下Theme.CustomDialog主题了:

<style name="Theme.CustomDialog" parent="android:style/Theme.Dialog">    <item name="android:windowBackground">@drawable/filled_box</item></style>

看到这里是不是就清楚怎么回事了?它继承了 Theme.Dialog 的样式,然后改变了 windowBackground 的颜色,仅此而已。在 style 的定义中是支持继承的,通过 parent 节点就可以实现,这样非常方便我们复用系统主题以及自己之前已经定义的主题,稍加修改就可以使用了。

这里我们看到它引用的是一个drawbl文件,跟进去看一下:

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

这又是一个资源文件,不过这次用的是 shape ,看一下官方文档的说明:

This is a generic shape defined in XML.

意思就是这是一个在xml里通用的定义形状的资源,看看各个节点的含义

  • shape 根节点 可以指定 shape 属性 不定的默认为矩形
  • solid 实线 可以指定 color
  • stroke 笔画的形状 就是最外围的那个深色的框 可以指定 width 和 color 等
  • corners 圆角 只能在 shape 指定为矩形时可以使用
  • padding 内边距

到这里我们这个 demo 就看完了,总的来说是相对简单的。看完这篇文章,在多看一些官方文档的说明,看可以完全自定义主题了。

0 0