第四篇: http://blog.csdn.net/mapdigit/article/details/7570371
先通过Android Manifest.xml文件找到 CustomDialogActivity :
我们看到CustomDialogActivity设置了一个Theme,去styles.xml看看是怎么定义的。
上面定义的Theme.CustomDialog主题继承了android.R.style.Theme.Dialog主题,同时覆盖了android:windowBackground属性。
我们去源代码文件/
frameworks / base / core / res / res / values / themes.xml下找到Theme.Dialog:
找到 CustomDialogActivity.java 只是设置了布局文件。
custom_dialog_activity.xml:
android:padding和android:layout_margin的区别?
App->Activity->Custom Dialog
这个Demo主要是实现自定义对话框。
先通过Android Manifest.xml文件找到 CustomDialogActivity :
1
<
activity
android:name
=".app.CustomDialogActivity"
2 android:label ="@string/activity_custom_dialog"
3 android:theme ="@style/Theme.CustomDialog" >
4 < intent-filter >
5 < action android:name ="android.intent.action.MAIN" />
6 < category android:name ="android.intent.category.SAMPLE_CODE" />
7 </ intent-filter >
8 </ activity >
2 android:label ="@string/activity_custom_dialog"
3 android:theme ="@style/Theme.CustomDialog" >
4 < intent-filter >
5 < action android:name ="android.intent.action.MAIN" />
6 < category android:name ="android.intent.category.SAMPLE_CODE" />
7 </ intent-filter >
8 </ activity >
我们看到CustomDialogActivity设置了一个Theme,去styles.xml看看是怎么定义的。
1
<!--
A theme for a custom dialog appearance. Here we use an ugly
2 custom frame. -->
3 < style name ="Theme.CustomDialog" parent ="android:style/Theme.Dialog" >
4 < item name ="android:windowBackground" > @drawable/filled_box </ item >
5 </ style >
2 custom frame. -->
3 < style name ="Theme.CustomDialog" parent ="android:style/Theme.Dialog" >
4 < item name ="android:windowBackground" > @drawable/filled_box </ item >
5 </ style >
Android应用可以使用自定义的界面风格(Theme),Theme 为一组相关的Style定义,可以应用于某个Activity或是整个Application。使用Theme的一个好处是可以为整个应用定义统一的界面风格(统一的背景色,字体等)。
定义Theme 和定义Style一样, 必须定义在/res/values子目录下,根元素名为resources, Theme 和Style的区别在于Theme应用于Activity和Application,而 Style应用于单个的View。 其定义方法是一致的。
上面定义的Theme.CustomDialog主题继承了android.R.style.Theme.Dialog主题,同时覆盖了android:windowBackground属性。
我们去源代码文件
1
<
style
name
="Theme.Dialog"
>
2 < item name ="android:windowFrame" > @null </ item >
3 < item name ="android:windowTitleStyle" > @android:style/DialogWindowTitle </ item >
4 < item name ="android:windowBackground" > @android:drawable/panel_background </ item >
5 < item name ="android:windowIsFloating" > true </ item >
6 < item name ="android:windowContentOverlay" > @null </ item >
7 < item name ="android:windowAnimationStyle" > @android:style/Animation.Dialog </ item >
8 < item name ="android:windowSoftInputMode" > stateUnspecified|adjustPan </ item >
9 < item name ="android:windowCloseOnTouchOutside" > @bool/config_closeDialogWhenTouchOutside </ item >
10 < item name ="android:windowActionModeOverlay" > true </ item >
11
12 < item name ="android:colorBackgroundCacheHint" > @null </ item >
13
14 < item name ="textAppearance" > @android:style/TextAppearance </ item >
15 < item name ="textAppearanceInverse" > @android:style/TextAppearance.Inverse </ item >
16
17 < item name ="textColorPrimary" > @android:color/primary_text_dark </ item >
18 < item name ="textColorSecondary" > @android:color/secondary_text_dark </ item >
19 < item name ="textColorTertiary" > @android:color/tertiary_text_dark </ item >
20 < item name ="textColorPrimaryInverse" > @android:color/primary_text_light </ item >
21 < item name ="textColorSecondaryInverse" > @android:color/secondary_text_light </ item >
22 < item name ="textColorTertiaryInverse" > @android:color/tertiary_text_light </ item >
23 < item name ="textColorPrimaryDisableOnly" > @android:color/primary_text_dark_disable_only </ item >
24 < item name ="textColorPrimaryInverseDisableOnly" > @android:color/primary_text_light_disable_only </ item >
25 < item name ="textColorPrimaryNoDisable" > @android:color/primary_text_dark_nodisable </ item >
26 < item name ="textColorSecondaryNoDisable" > @android:color/secondary_text_dark_nodisable </ item >
27 < item name ="textColorPrimaryInverseNoDisable" > @android:color/primary_text_light_nodisable </ item >
28 < item name ="textColorSecondaryInverseNoDisable" > @android:color/secondary_text_light_nodisable </ item >
29 < item name ="textColorHint" > @android:color/hint_foreground_dark </ item >
30 < item name ="textColorHintInverse" > @android:color/hint_foreground_light </ item >
31 < item name ="textColorSearchUrl" > @android:color/search_url_text </ item >
32
33 < item name ="textAppearanceLarge" > @android:style/TextAppearance.Large </ item >
34 < item name ="textAppearanceMedium" > @android:style/TextAppearance.Medium </ item >
35 < item name ="textAppearanceSmall" > @android:style/TextAppearance.Small </ item >
36 < item name ="textAppearanceLargeInverse" > @android:style/TextAppearance.Large.Inverse </ item >
37 < item name ="textAppearanceMediumInverse" > @android:style/TextAppearance.Medium.Inverse </ item >
38 < item name ="textAppearanceSmallInverse" > @android:style/TextAppearance.Small.Inverse </ item >
39
40 < item name ="listPreferredItemPaddingLeft" > 10dip </ item >
41 < item name ="listPreferredItemPaddingRight" > 10dip </ item >
42 </ style >
2 < item name ="android:windowFrame" > @null </ item >
3 < item name ="android:windowTitleStyle" > @android:style/DialogWindowTitle </ item >
4 < item name ="android:windowBackground" > @android:drawable/panel_background </ item >
5 < item name ="android:windowIsFloating" > true </ item >
6 < item name ="android:windowContentOverlay" > @null </ item >
7 < item name ="android:windowAnimationStyle" > @android:style/Animation.Dialog </ item >
8 < item name ="android:windowSoftInputMode" > stateUnspecified|adjustPan </ item >
9 < item name ="android:windowCloseOnTouchOutside" > @bool/config_closeDialogWhenTouchOutside </ item >
10 < item name ="android:windowActionModeOverlay" > true </ item >
11
12 < item name ="android:colorBackgroundCacheHint" > @null </ item >
13
14 < item name ="textAppearance" > @android:style/TextAppearance </ item >
15 < item name ="textAppearanceInverse" > @android:style/TextAppearance.Inverse </ item >
16
17 < item name ="textColorPrimary" > @android:color/primary_text_dark </ item >
18 < item name ="textColorSecondary" > @android:color/secondary_text_dark </ item >
19 < item name ="textColorTertiary" > @android:color/tertiary_text_dark </ item >
20 < item name ="textColorPrimaryInverse" > @android:color/primary_text_light </ item >
21 < item name ="textColorSecondaryInverse" > @android:color/secondary_text_light </ item >
22 < item name ="textColorTertiaryInverse" > @android:color/tertiary_text_light </ item >
23 < item name ="textColorPrimaryDisableOnly" > @android:color/primary_text_dark_disable_only </ item >
24 < item name ="textColorPrimaryInverseDisableOnly" > @android:color/primary_text_light_disable_only </ item >
25 < item name ="textColorPrimaryNoDisable" > @android:color/primary_text_dark_nodisable </ item >
26 < item name ="textColorSecondaryNoDisable" > @android:color/secondary_text_dark_nodisable </ item >
27 < item name ="textColorPrimaryInverseNoDisable" > @android:color/primary_text_light_nodisable </ item >
28 < item name ="textColorSecondaryInverseNoDisable" > @android:color/secondary_text_light_nodisable </ item >
29 < item name ="textColorHint" > @android:color/hint_foreground_dark </ item >
30 < item name ="textColorHintInverse" > @android:color/hint_foreground_light </ item >
31 < item name ="textColorSearchUrl" > @android:color/search_url_text </ item >
32
33 < item name ="textAppearanceLarge" > @android:style/TextAppearance.Large </ item >
34 < item name ="textAppearanceMedium" > @android:style/TextAppearance.Medium </ item >
35 < item name ="textAppearanceSmall" > @android:style/TextAppearance.Small </ item >
36 < item name ="textAppearanceLargeInverse" > @android:style/TextAppearance.Large.Inverse </ item >
37 < item name ="textAppearanceMediumInverse" > @android:style/TextAppearance.Medium.Inverse </ item >
38 < item name ="textAppearanceSmallInverse" > @android:style/TextAppearance.Small.Inverse </ item >
39
40 < item name ="listPreferredItemPaddingLeft" > 10dip </ item >
41 < item name ="listPreferredItemPaddingRight" > 10dip </ item >
42 </ style >
它定义了很多属性,就不详细讲了。我们再去看看filled_box.xml是怎么定义的。
1
<?
xml version="1.0" encoding="utf-8"
?>
2 <!--
3 ** Copyright 2007, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 -->
18
19 < shape xmlns:android ="http://schemas.android.com/apk/res/android" >
20 < solid android:color ="#f0600000" />
21 < stroke android:width ="3dp" color ="#ffff8080" />
22 < corners android:radius ="3dp" />
23 < padding android:left ="10dp" android:top ="10dp"
24 android:right ="10dp" android:bottom ="10dp" />
25 </ shape >
2 <!--
3 ** Copyright 2007, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 -->
18
19 < shape xmlns:android ="http://schemas.android.com/apk/res/android" >
20 < solid android:color ="#f0600000" />
21 < stroke android:width ="3dp" color ="#ffff8080" />
22 < corners android:radius ="3dp" />
23 < padding android:left ="10dp" android:top ="10dp"
24 android:right ="10dp" android:bottom ="10dp" />
25 </ shape >
1
Shape
2 简介
3 作用:XML中定义的几何形状
4 位置:res/drawable/文件的名称.xml
5 使用的方法:
6 Java代码中:R.drawable.文件的名称
7 XML中:Android:background = " @drawable/文件的名称 "
8 属性:
9 <shape> Android:shape = [ "rectangle" | "oval" | "line" | "ring" ]
10 其中rectagle矩形,oval椭圆,line水平直线,ring环形
11 <shape>中子节点的常用属性:
12 <gradient> 渐变
13 Android:startColor 起始颜色
14 Android:endColor 结束颜色
15 Android:angle 渐变角度,0从上到下,90表示从左到右,数值为45的整数倍默认为0;
16 Android:type 渐变的样式 liner线性渐变 radial环形渐变 sweep
17 <solid > 填充
18 Android:color 填充的颜色
19 <stroke > 描边
20 Android:width 描边的宽度
21 Android:color 描边的颜色
22 Android:dashWidth 表示'-'横线的宽度
23 Android:dashGap 表示'-'横线之间的距离
24 <corners > 圆角
25 Android:radius 圆角的半径 值越大角越圆
26 Android:topRightRadius 右上圆角半径
27 Android:bottomLeftRadius 右下圆角角半径
28 Android:topLeftRadius 左上圆角半径
29 Android:bottomRightRadius 左下圆角半径
2 简介
3 作用:XML中定义的几何形状
4 位置:res/drawable/文件的名称.xml
5 使用的方法:
6 Java代码中:R.drawable.文件的名称
7 XML中:Android:background = " @drawable/文件的名称 "
8 属性:
9 <shape> Android:shape = [ "rectangle" | "oval" | "line" | "ring" ]
10 其中rectagle矩形,oval椭圆,line水平直线,ring环形
11 <shape>中子节点的常用属性:
12 <gradient> 渐变
13 Android:startColor 起始颜色
14 Android:endColor 结束颜色
15 Android:angle 渐变角度,0从上到下,90表示从左到右,数值为45的整数倍默认为0;
16 Android:type 渐变的样式 liner线性渐变 radial环形渐变 sweep
17 <solid > 填充
18 Android:color 填充的颜色
19 <stroke > 描边
20 Android:width 描边的宽度
21 Android:color 描边的颜色
22 Android:dashWidth 表示'-'横线的宽度
23 Android:dashGap 表示'-'横线之间的距离
24 <corners > 圆角
25 Android:radius 圆角的半径 值越大角越圆
26 Android:topRightRadius 右上圆角半径
27 Android:bottomLeftRadius 右下圆角角半径
28 Android:topLeftRadius 左上圆角半径
29 Android:bottomRightRadius 左下圆角半径
找到 CustomDialogActivity.java 只是设置了布局文件。
1
/**/
/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.example.android.apis.app;
18
19 // Need the following import to get access to the app resources, since this
20 // class is in a sub-package.
21 import com.example.android.apis.R;
22
23 import android.app.Activity;
24 import android.os.Bundle;
25
26 /** */ /**
27 * <h3>Dialog Activity</h3>
28 *
29 * <p>This demonstrates the how to write an activity that looks like
30 * a pop-up dialog with a custom theme using a different text color.</p>
31 */
32 public class CustomDialogActivity extends Activity {
33 /** *//**
34 * Initialization of the Activity after it is first created. Must at least
35 * call {@link android.app.Activity#setContentView setContentView()} to
36 * describe what is to be displayed in the screen.
37 */
38 @Override
39 protected void onCreate(Bundle savedInstanceState) {
40 // Be sure to call the super class.
41 super.onCreate(savedInstanceState);
42
43 // See assets/res/any/layout/dialog_activity.xml for this
44 // view layout definition, which is being set here as
45 // the content of our screen.
46 setContentView(R.layout.custom_dialog_activity);
47 }
48}
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.example.android.apis.app;
18
19 // Need the following import to get access to the app resources, since this
20 // class is in a sub-package.
21 import com.example.android.apis.R;
22
23 import android.app.Activity;
24 import android.os.Bundle;
25
26 /** */ /**
27 * <h3>Dialog Activity</h3>
28 *
29 * <p>This demonstrates the how to write an activity that looks like
30 * a pop-up dialog with a custom theme using a different text color.</p>
31 */
32 public class CustomDialogActivity extends Activity {
33 /** *//**
34 * Initialization of the Activity after it is first created. Must at least
35 * call {@link android.app.Activity#setContentView setContentView()} to
36 * describe what is to be displayed in the screen.
37 */
38 @Override
39 protected void onCreate(Bundle savedInstanceState) {
40 // Be sure to call the super class.
41 super.onCreate(savedInstanceState);
42
43 // See assets/res/any/layout/dialog_activity.xml for this
44 // view layout definition, which is being set here as
45 // the content of our screen.
46 setContentView(R.layout.custom_dialog_activity);
47 }
48}
custom_dialog_activity.xml:
1
<?
xml version="1.0" encoding="utf-8"
?>
2 <!-- Copyright (C) 2008 The Android Open Source Project
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 -->
16
17 <!-- Demonstrates an activity with a custom dialog theme.
18 See corresponding Java code com.android.sdk.app.CustomDialogActivity.java. -->
19
20 <!-- This screen consists of a single text field that displays some text. -->
21 < TextView xmlns:android ="http://schemas.android.com/apk/res/android" android:id ="@+id/text"
22 android:layout_width ="match_parent" android:layout_height ="match_parent"
23 android:gravity ="center_vertical|center_horizontal"
24 android:text ="@string/custom_dialog_activity_text" />
2 <!-- Copyright (C) 2008 The Android Open Source Project
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 -->
16
17 <!-- Demonstrates an activity with a custom dialog theme.
18 See corresponding Java code com.android.sdk.app.CustomDialogActivity.java. -->
19
20 <!-- This screen consists of a single text field that displays some text. -->
21 < TextView xmlns:android ="http://schemas.android.com/apk/res/android" android:id ="@+id/text"
22 android:layout_width ="match_parent" android:layout_height ="match_parent"
23 android:gravity ="center_vertical|center_horizontal"
24 android:text ="@string/custom_dialog_activity_text" />
android:padding和android:layout_margin的区别?
android:layout_margin就是设置view的上下左右边框的额外空间 android:padding是设置内容相对view的边框的距离 |
在LinearLayout、RelativeLayout、TableLayout中,这2个属性都是设置都是有效的
在FrameLayout中,android:layout_margin是无效的,因为FrameLayout里面的元素都是从左上角开始绘制的