在我们的日常项目中很多地方会用到对话框,但是Android系统为我们提供的对话框样子和我们精心设计的界面很不协调,在这种情况下我们想很自由的定义对话框,或者有的时候我们的对话框是一个图片,没有标题和按钮,例如这样的一系列需求,这一篇文章我们来给大家介绍一下如何像使用Activity一样来自定义我们的对话框。

一般自定义对话框有下面几种办法:

1、重写Dialog来实现。

2、获取Dialog的Window对象实现。

3、使用WindowManager来实现。

4、使用DialogTheme来实现。

下面我们来介绍一下第二种和第四种方式,并且将封装好的代码贴出,第三种方式待后面对WindowManager进行介绍的时候再说。

转载请说明出处:http://blog.csdn.net/dawanganban

一、自定义Dialog

主界面布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/dialog_custom_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义对话框"/>
<Button
android:id="@+id/dialog_activity_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity对话框"/>
</LinearLayout>

用两个按钮来分别弹出两种类型的对话框

public class MainActivity extends Activity implements OnClickListener{

	@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.dialog_custom_button).setOnClickListener(this);
findViewById(R.id.dialog_activity_button).setOnClickListener(this);
} @Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.dialog_custom_button:
DialogCustom dc = new DialogCustom(MainActivity.this, R.layout.dialog);
dc.setDismissButtonId(R.id.close_dialog);
break;
case R.id.dialog_activity_button:
Intent intent = new Intent(MainActivity.this, DialogActivity.class);
startActivity(intent);
break;
default:
break;
}
}
}

对自定义对话框的方法进行了简单封装,方便使用,可以依据你的项目需求进行封装。

package com.example.testcustomdialog;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window; /**
* 公用的自定义对话框
* @author Administrator
*
*/
public class DialogCustom{ private android.app.AlertDialog.Builder builder;
private int layout;
private AlertDialog dialog;
private Window window; public DialogCustom(Context context, int layout){
builder = new AlertDialog.Builder(context);
this.layout = layout;
} public DialogCustom(Context context, int theme, int layout){
builder = new AlertDialog.Builder(context, theme);
this.layout = layout;
} public Builder getBuilder(){
return builder;
} /**
* 获取对话框的Window对象
* @return
*/
public Window getWindow(){
dialog = builder.create();
dialog.show();
window = dialog.getWindow();
window.setContentView(layout);
return window;
} /**
* 通过ID获取对应的View
* @param id
* @return
*/
public View getViewById(int id){
if(window == null) getWindow();
return window.findViewById(id);
} /**
* 设置需要添加关闭事件的按钮ID
* @param id
*/
public void setDismissButtonId(int id){
View view = getViewById(id);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
} /**
* 关闭对话框
*/
public void dismiss(){
if(dialog != null){
dialog.dismiss();
}
}
}

然后创建Dialog对象添加监听View就可以了,主要工作在布局文件中,也就是你的对话框的内容。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:paddingBottom="30dip"
android:background="#ffffff">
<TextView
android:layout_width="260dip"
android:layout_height="40dip"
android:layout_margin="30dip"
android:gravity="center"
android:text="这是ActivityDialog"/>
<Button
android:id="@+id/close_dialog"
android:layout_width="100dip"
android:layout_height="30dip"
android:text="点击关闭"
android:background="@drawable/close_butten_p"/>
</LinearLayout>
</LinearLayout>

二、使用DialogTheme

    <style name="DialogActivityTheme" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:width">1px</item>
<item name="android:height">1px</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:backgroundDimEnabled">true</item>
</style>

在ActivityManifest.xml中注册DialogActivity

        <activity
android:name="com.example.testcustomdialog.DialogActivity"
android:theme="@style/DialogActivityTheme"
android:screenOrientation="portrait"></activity>

这样我们就可以像使用普通Activity一样创建一个对话框了,是不是很简单。

三、两种对话框总结

运行起来看似一样的对话框却有很大区别,使用第二种方式创建的对话框其实是一个Activity,所以具有Activity的生命周期和所有特性。它会影响到栈中的其他Activity的生命周期。而且如果在对话框中有需要输入的输入框,建议使用这种方式,可以自动调出输入法并使屏幕自动适应,而第一种方式的对话框创建特别方便,在需要显示一段提示信息的时候建议使用。(源代码下载:http://download.csdn.net/detail/lxq_xsyu/8315023

CSDN博客之星活动开始了,为我投上一票吧:http://vote.blog.csdn.net/blogstar2014/details?username=lxq_xsyu#content

Android自定义组件系列【13】——Android自定义对话框如此简单的更多相关文章

  1. Android自定义组件系列【5】——进阶实践(2)

    上一篇<Android自定义组件系列[5]--进阶实践(1)>中对任老师的<可下拉的PinnedHeaderExpandableListView的实现>前一部分进行了实现,这一 ...

  2. Android自定义组件系列【7】——进阶实践(4)

    上一篇<Android自定义组件系列[6]--进阶实践(3)>中补充了关于Android中事件分发的过程知识,这一篇我们接着来分析任老师的<可下拉的PinnedHeaderExpan ...

  3. Android自定义组件系列【6】——进阶实践(3)

    上一篇<Android自定义组件系列[5]--进阶实践(2)>继续对任老师的<可下拉的PinnedHeaderExpandableListView的实现>进行了分析,这一篇计划 ...

  4. Android自定义组件系列【4】——自定义ViewGroup实现双侧滑动

    在上一篇文章<Android自定义组件系列[3]--自定义ViewGroup实现侧滑>中实现了仿Facebook和人人网的侧滑效果,这一篇我们将接着上一篇来实现双面滑动的效果. 1.布局示 ...

  5. Android自定义组件系列【3】——自定义ViewGroup实现侧滑

    有关自定义ViewGroup的文章已经很多了,我为什么写这篇文章,对于初学者或者对自定义组件比较生疏的朋友虽然可以拿来主义的用了,但是要一步一步的实现和了解其中的过程和原理才能真真脱离别人的代码,举一 ...

  6. Android自定义组件系列【17】——教你如何高仿微信录音Toast

    一.Toast介绍 平时我们在Android开发中会经常用到一个叫Toast的东西,官方解释如下 A toast is a view containing a quick little message ...

  7. Android自定义组件系列【8】——遮罩文字动画

    遮罩文字的动画我们在Flash中非常常见,作为Android的应用开发者你是否也想将这种动画做到你的应用中去呢?这一篇文章我们来看看如何自定义一个ImageView来实现让一张文字图片实现文字的遮罩闪 ...

  8. Android自定义组件系列【5】——进阶实践(1)

    接下来几篇文章将对任老师的博文<可下拉的PinnedHeaderExpandableListView的实现>分步骤来详细实现,来学习一下大神的代码并记录一下. 原文出处:http://bl ...

  9. Android自定义组件系列【2】——Scroller类

    在上一篇中介绍了View类的scrollTo和scrollBy两个方法,对这两个方法不太了解的朋友可以先看<自定义View及ViewGroup> scrollTo和scrollBy虽然实现 ...

随机推荐

  1. 解决夸dll返回dynamic无法访问

    public static class DynamicFactory { //创建线程安全(对象不会再同一时刻被多个线程访问)的字典对象 private static ConcurrentDictio ...

  2. PostgreSQL Replication之第三章 理解即时恢复(2)

    3.2 归档事务日志 看过图片之后,我们可以看看如何使这些东西进入工作状态.当谈到及时归档时,您需要做的第一件事是归档XLOG.PostgreSQL通过postgresql.conf提供了所有与归档相 ...

  3. 515Nod 1126 求递推序列的第n项【矩阵快速幂】

    有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...

  4. LVS+keepalived均衡nginx配置

    如果单台LVS发生突发情况,例如宕机.发生不可恢复现象,会导致用户无法访问后端所有的应用程序.避免这种问题可以使用HA故障切换,也就是有一台备用的LVS,主LVS 宕机,LVS VIP自动切换到从,可 ...

  5. centeros 7配置mailx使用外部smtp服务器发送邮件

    发送邮件的两种方式: 1.连接现成的smtp服务器去发送(此方法比较简单,直接利用现有的smtp服务器比如qq.新浪.网易等邮箱,只需要直接配置mail.rc文件即可实现) 2.自己搭建私有的smtp ...

  6. Java中发送http的get、post请求

    近期做项目中,须要把消息通过中间件的形式通过http请求的方式推送给第三方,因此用到了http协议,小编花费了一个多小时.对于http协议中的post和get请求,封装了一个工具类.以下与大家分享一下 ...

  7. nginx和apache

    apache所占用的内存资源较多,并且处理较慢 apache的全部模块都支持动静态编译 apache对Fcgi的支持不好 apache不支持epoll apache相对于nginx是一个庞然大物 ng ...

  8. IOS--文件管理NSFileManager

    iOS的沙盒机制.应用仅仅能訪问自己应用文件夹下的文件.iOS不像android.没有SD 卡概念.不能直接訪问图像.视频等内容. iOS应用产生的内容,如图像.文件.缓存内容等都必须存储在自己的沙盒 ...

  9. vue.2.0-路由

    vue2.0 路由: http://router.vuejs.org/zh-cn/index.html 基本使用: 1. 布局 <router-link to="/home" ...

  10. Install the IIS 6.0 Management Compatibility Components in Windows 7 or in Windows Vista from Control Panel

    https://technet.microsoft.com/en-us/library/bb397374(v=exchg.80).aspx Install the IIS 6.0 Management ...