Android自定义Dialog对话框
来源:互联网 发布:国企的工资待遇知乎 编辑:程序博客网 时间:2024/04/29 21:53
转自:http://www.itivy.com/android/archive/2011/11/3/android-dialog-customize.html
很多时候,可能Android默认的几种Dialog对话框已经不能满足我们的需求,我们需要自定义自己的Dialog对话框,包括样式的改变,功能的改变等等。今天,我给出一个Android自定义Dialog的例子,大家可以看看,如果这个自定义dialog的方法能够用得上,那我们完全可以定义出非常富有个性的dialog对话框了,先看一个自定义的dialog对话框效果图吧,很简单,只有一个Activity,当点击Button的时候就弹出这个自定义的Dialog
里面的几张图都比较丑,我不多会美工,随便用powerpoint画了几张图,原理是一样的,先不计较这些。下面正入正题
为了照顾到所有的码农,在些把所有的代码都贴出来
新建工程在此就不贴出来了,只是为了方便大家的复制粘贴,取包名为com.and.mydialog,主Activity取名为MyDialogActivity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package
com.and.mydialog;
import
android.app.Activity;
import
android.app.AlertDialog;
import
android.app.Dialog;
import
android.content.DialogInterface;
import
android.os.Bundle;
import
android.view.KeyEvent;
import
android.view.LayoutInflater;
import
android.view.Menu;
import
android.view.MenuItem;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.Toast;
public
class
MyDialogActivity
extends
Activity {
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View v) {
//初始化一个自定义的Dialog
Dialog dialog =
new
MyDialog(MyDialogActivity.
this
,
R.style.MyDialog);
dialog.show();
}
});
}
}
1
2
3
4
5
6
7
8
9
10
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:orientation
=
"vertical"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
>
<
Button
android:text
=
"显示自定义Dialog"
android:id
=
"@+id/button1"
android:layout_height
=
"wrap_content"
android:layout_width
=
"fill_parent"
/>
</
LinearLayout
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package
com.and.mydialog;
import
android.app.Dialog;
import
android.content.Context;
import
android.os.Bundle;
import
android.view.LayoutInflater;
import
android.view.View;
public
class
MyDialog
extends
Dialog {
Context context;
public
MyDialog(Context context) {
super
(context);
// TODO Auto-generated constructor stub
this
.context = context;
}
public
MyDialog(Context context,
int
theme){
super
(context, theme);
this
.context = context;
}
@Override
protected
void
onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super
.onCreate(savedInstanceState);
this
.setContentView(R.layout.dialog);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:orientation
=
"vertical"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:gravity
=
"center_vertical|center_horizontal"
android:background
=
"@drawable/dialog_bg"
>
<
RelativeLayout
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:paddingLeft
=
"30dip"
android:paddingTop
=
"10dip"
>
<
ImageView
android:id
=
"@+id/dialog_title_image"
android:layout_alignParentLeft
=
"true"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:background
=
"@drawable/dialog_title_image"
/>
<
TextView
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_marginLeft
=
"10dip"
android:layout_centerInParent
=
"true"
android:text
=
"Title"
android:layout_toRightOf
=
"@id/dialog_title_image"
android:textColor
=
"#000000"
android:textSize
=
"30sp"
/>
</
RelativeLayout
>
<
TextView
android:layout_width
=
"fill_parent"
android:layout_height
=
"1dip"
android:background
=
"@drawable/lins"
android:layout_marginTop
=
"5dip"
/>
<
TextView
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"This is a custom dialog"
android:textColor
=
"#000000"
android:layout_marginTop
=
"10dip"
android:layout_marginLeft
=
"30dip"
/>
<
RelativeLayout
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:paddingTop
=
"10dip"
android:gravity
=
"bottom|center_horizontal"
android:paddingBottom
=
"10dip"
>
<
Button
android:id
=
"@+id/dialog_button_cancel"
android:layout_alignParentLeft
=
"true"
android:layout_width
=
"100dip"
android:layout_height
=
"wrap_content"
android:text
=
"确定"
/>
<
Button
android:id
=
"@+id/dialog_button_ok"
android:layout_width
=
"100dip"
android:layout_height
=
"wrap_content"
android:layout_toRightOf
=
"@id/dialog_button_cancel"
android:layout_marginLeft
=
"35dip"
android:text
=
"取消"
/>
</
RelativeLayout
>
</
LinearLayout
>
最主要的,是自定义的Style,我们自定义一个式样,用来改变默认的Dialog样式
在values文件夹下新建一个styles.xml文件
1
2
3
4
5
6
7
8
9
10
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
resources
>
<
style
name
=
"MyDialog"
parent
=
"@android:Theme.Dialog"
>
<
item
name
=
"android:windowFrame"
>@null</
item
>
<
item
name
=
"android:windowNoTitle"
>true</
item
>
<
item
name
=
"android:windowBackground"
>@drawable/dialog_bg</
item
>
<
item
name
=
"android:windowIsFloating"
>true</
item
>
<
item
name
=
"android:windowContentOverlay"
>@null</
item
>
</
style
>
</
resources
>
好了,这样我们的自定义dialog就基本完成了,虽然是难看了点,但是基本思路就是这样了,美工好的同学可以用这个思路去写一个比较漂亮的android自定义dialog对话框。
- Android Dialog( 自定义对话框)
- Android自定义Dialog对话框
- Android 自定义Dialog对话框
- Android 自定义对话框Dialog
- Android自定义对话框(Dialog)
- 【android】自定义dialog对话框
- Android 自定义对话框dialog
- android 自定义对话框 Dialog
- Android自定义Dialog对话框
- Android中自定义对话框(Dialog)
- Android中自定义对话框(Dialog)
- Android自定义对话框(Custom Dialog)
- android 继承dialog自定义对话框
- Android中自定义对话框(Dialog)
- Android中自定义对话框Dialog
- Android深度自定义Dialog对话框
- Android dialog 对话框自定义布局
- Android 自定义Dialog对话框详解
- 图的 深度优先搜索(DFS) 以及 广度优先搜索(BFS)
- eclipse开发项目
- Incorrect integer value: ‘ for column “ID” at row 1
- 结合SQL Server全文检索对Word内容进行检索的三个方案
- 关于intent and intent-filter资料
- Android自定义Dialog对话框
- Java栈与堆
- va_start va_arg va_end 的使用和原理
- test
- cocoachina ios 开发资料整理
- 在C语言程序中使用cURL库(libcurl)
- jquery 中html 、val 与text三者属性取值的联系与区别
- V-STARS系统
- java 字节流的个人理解