Android自定义Dialog对话框

转自: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();
             }
         });
 
     }
}
主布局文件main.xml
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 >
新建一个自定义的Dialog类,取名MyDialog,继承自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
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);
     }
 
}
相应的布局文件dialog.xml
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 >
这样应该就OK了,为了方便大家测试本示例,在此一并附上不怎么好看的素材


好了,这样我们的自定义dialog就基本完成了,虽然是难看了点,但是基本思路就是这样了,美工好的同学可以用这个思路去写一个比较漂亮的android自定义dialog对话框。


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值