Android自定义组件系列【13】——Android自定义对话框如此简单
在我们的日常项目中很多地方会用到对话框,但是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自定义对话框如此简单的更多相关文章
- Android自定义组件系列【5】——进阶实践(2)
上一篇<Android自定义组件系列[5]--进阶实践(1)>中对任老师的<可下拉的PinnedHeaderExpandableListView的实现>前一部分进行了实现,这一 ...
- Android自定义组件系列【7】——进阶实践(4)
上一篇<Android自定义组件系列[6]--进阶实践(3)>中补充了关于Android中事件分发的过程知识,这一篇我们接着来分析任老师的<可下拉的PinnedHeaderExpan ...
- Android自定义组件系列【6】——进阶实践(3)
上一篇<Android自定义组件系列[5]--进阶实践(2)>继续对任老师的<可下拉的PinnedHeaderExpandableListView的实现>进行了分析,这一篇计划 ...
- Android自定义组件系列【4】——自定义ViewGroup实现双侧滑动
在上一篇文章<Android自定义组件系列[3]--自定义ViewGroup实现侧滑>中实现了仿Facebook和人人网的侧滑效果,这一篇我们将接着上一篇来实现双面滑动的效果. 1.布局示 ...
- Android自定义组件系列【3】——自定义ViewGroup实现侧滑
有关自定义ViewGroup的文章已经很多了,我为什么写这篇文章,对于初学者或者对自定义组件比较生疏的朋友虽然可以拿来主义的用了,但是要一步一步的实现和了解其中的过程和原理才能真真脱离别人的代码,举一 ...
- Android自定义组件系列【17】——教你如何高仿微信录音Toast
一.Toast介绍 平时我们在Android开发中会经常用到一个叫Toast的东西,官方解释如下 A toast is a view containing a quick little message ...
- Android自定义组件系列【8】——遮罩文字动画
遮罩文字的动画我们在Flash中非常常见,作为Android的应用开发者你是否也想将这种动画做到你的应用中去呢?这一篇文章我们来看看如何自定义一个ImageView来实现让一张文字图片实现文字的遮罩闪 ...
- Android自定义组件系列【5】——进阶实践(1)
接下来几篇文章将对任老师的博文<可下拉的PinnedHeaderExpandableListView的实现>分步骤来详细实现,来学习一下大神的代码并记录一下. 原文出处:http://bl ...
- Android自定义组件系列【2】——Scroller类
在上一篇中介绍了View类的scrollTo和scrollBy两个方法,对这两个方法不太了解的朋友可以先看<自定义View及ViewGroup> scrollTo和scrollBy虽然实现 ...
随机推荐
- tf.nn.softmax(logits,name=None)
tf.nn.softmax( logits, axis=None, name=None, dim=None #dim在后来改掉了 ) 通过Softmax回归,将logistic的预测二分类的概率的问题 ...
- vue引入iconfont阿里字体图标库以及报错解决
下载阿里的字体图标库文件,放在\src\assets\font文件夹下面. 安装style-loader,css-loader和file-loader (或url-loader) ,记得--save ...
- [笔记-图论]Dijkstra
用于求正权有向图 上的 单源最短路 优化后时间复杂度O(mlogn) 模板 // Dijkstra // to get the minumum distance with no negtive way ...
- Kubernetes本地私有仓库配置
实验环境 master 10.6.191.181 node1 10.6.191.182 node2 10.6.191.183 本地私有仓库 10.6.191.184 一.安装本地私有仓库 1.安装do ...
- 移动端 Modal 组件开发杂谈
Vant 是有赞开发的一套基于 Vue 2.0 的 Mobile 组件库,在开发的过程中也踩了很多坑,今天我们就来聊一聊开发一个移动端 Modal 组件(在有赞该组件被称为 Popup )需要注意的一 ...
- 安卓Gallery配合ImageSwitcher不显示图片
Gallary装的是缩略图(thumb),ImageSwitcher装的是大图. 不显示图片的一个可能原因是gallery没设置代理器,另一个原因是没使用相对布局. GalleryActivity.j ...
- 怎样用第三方开源免费软件portecle从https站点上导出SSL的CA证书?
在我这篇文章中.我提到了怎样用OpenSSL从https站点上导出SSL的CA证书? 这样的方式不太直观,且须要用户自己手工拷贝.然后另存为文件,那么有没有更好更方便的工具呢? 幸运的是,有热心于开 ...
- poj 2240 Bellman-Flod 求环
http://poj.org/problem?id=2240 深刻体现了自己代码能力有问题外加改模板能力有问题.外加Debug有问题.以后做到: 1.算法原理能够轻易弄出来. 2.代码模板自己收集各种 ...
- 【POJ 2195】 Going Home(KM算法求最小权匹配)
[POJ 2195] Going Home(KM算法求最小权匹配) Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
- 在Maven项目中关于SSM框架中邮箱验证登陆
1.你如果要在maven项目中进行邮箱邮箱验证,你首先要先到pom.xml文件中配置mail.jar,activation.jar包 <dependency> <groupId> ...