Android开发之自定义dialog的实现(源代码分享)

    使用系统自带的dialog如果不能满足我们日常开发的需求,那就得自己构建custom dialog,特别是对于一个app来说,统一的样式风格会给人一种舒服的感觉,所以dialog的样式 、色调一般都要和app主题符合,这篇博客主要介绍两种方式来自定义dialog。

MainActivity的代码

[java]  view plain copy
  1. package com.example.e01_consumerdialog;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.app.AlertDialog;  
  6. import android.app.AlertDialog.Builder;  
  7. import android.content.DialogInterface;  
  8. import android.content.DialogInterface.OnClickListener;  
  9. import android.view.LayoutInflater;  
  10. import android.view.Menu;  
  11. import android.view.View;  
  12. import android.widget.Button;  
  13.   
  14. public class MainActivity extends Activity {  
  15.      private Button button;  
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.         button=(Button)this.findViewById(R.id.button1);  
  21.         button.setOnClickListener(new View.OnClickListener() {  
  22.               
  23.             @Override  
  24.             public void onClick(View arg0) {  
  25.                 // TODO Auto-generated method stub  
  26.               AlertDialog.Builder builder=new Builder(MainActivity.this);  
  27.               builder.setTitle("登陆界面");  
  28.               View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog, null);  
  29.               builder.setView(view);//设置自定义布局view  
  30.               builder.setPositiveButton("确认"new OnClickListener() {  
  31.                   
  32.                 @Override  
  33.                 public void onClick(DialogInterface arg0, int arg1) {  
  34.                     // TODO Auto-generated method stub  
  35.                       
  36.                 }  
  37.             });  
  38.               builder.setNegativeButton("取消",new OnClickListener() {  
  39.                   
  40.                 @Override  
  41.                 public void onClick(DialogInterface arg0, int arg1) {  
  42.                     // TODO Auto-generated method stub  
  43.                       
  44.                 }  
  45.             });  
  46.               builder.show();  
  47.             }  
  48.         });  
  49.     }  
  50.   
  51.   
  52.     @Override  
  53.     public boolean onCreateOptionsMenu(Menu menu) {  
  54.         // Inflate the menu; this adds items to the action bar if it is present.  
  55.         getMenuInflater().inflate(R.menu.main, menu);  
  56.         return true;  
  57.           
  58.     }  
  59.       
  60. }  

dialog.xml的布局文件

[java]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <TextView  
  7.         android:id="@+id/textView2"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_below="@+id/editText1"  
  11.         android:layout_marginTop="27dp"  
  12.         android:layout_toLeftOf="@+id/editText1"  
  13.         android:text="密  码:" />  
  14.   
  15.     <EditText  
  16.         android:id="@+id/editText2"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_alignBaseline="@+id/textView2"  
  20.         android:layout_alignBottom="@+id/textView2"  
  21.         android:layout_alignLeft="@+id/editText1"  
  22.         android:layout_alignRight="@+id/editText1"  
  23.         android:ems="10"  
  24.         android:inputType="textPassword" />  
  25.   
  26.     <EditText  
  27.         android:id="@+id/editText1"  
  28.         android:layout_width="wrap_content"  
  29.         android:layout_height="wrap_content"  
  30.         android:layout_alignBaseline="@+id/textView1"  
  31.         android:layout_alignBottom="@+id/textView1"  
  32.         android:layout_toRightOf="@+id/textView1"  
  33.         android:ems="10" />  
  34.   
  35.     <TextView  
  36.         android:id="@+id/textView1"  
  37.         android:layout_width="wrap_content"  
  38.         android:layout_height="wrap_content"  
  39.         android:layout_alignParentLeft="true"  
  40.         android:layout_alignParentTop="true"  
  41.         android:text="用户名:" />  
  42.       
  43. </RelativeLayout>  

   以上的自定义dialog是模拟实现了一个登陆框选项的功能,但缺点也比较明显,按钮必须还得使用dialog本身的 PositiveButton这种风格,那有没有使我们的权限更大一点的自定义方法呢,从官方API我们可以看到

Tip: If you want a custom dialog, you can instead display an Activity as a dialog instead of using the DialogAPIs. Simply create an activity and set its theme to Theme.Holo.Dialog in the <activity> manifest element:

<activity android:theme="@android:style/Theme.Holo.Dialog" >

That's it. The activity now displays in a dialog window instead of fullscreen.

    上面的tip告诉我们如果想自定义dialog,还可以把自己做的activity在清单文件中添加dialog属性,以此实现dialog形式的出现效果!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值