Android开发之自定义dialog的实现(源代码分享)
使用系统自带的dialog如果不能满足我们日常开发的需求,那就得自己构建custom dialog,特别是对于一个app来说,统一的样式风格会给人一种舒服的感觉,所以dialog的样式 、色调一般都要和app主题符合,这篇博客主要介绍两种方式来自定义dialog。
MainActivity的代码
package com.example.e01_consumerdialog;import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.Button;public class MainActivity extends Activity {private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button=(Button)this.findViewById(R.id.button1);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubAlertDialog.Builder builder=new Builder(MainActivity.this);builder.setTitle("登陆界面");View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog, null);builder.setView(view);//设置自定义布局viewbuilder.setPositiveButton("确认", new OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {// TODO Auto-generated method stub}});builder.setNegativeButton("取消",new OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {// TODO Auto-generated method stub}});builder.show();}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
dialog.xml的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/editText1"android:layout_marginTop="27dp"android:layout_toLeftOf="@+id/editText1"android:text="密 码:" /><EditTextandroid:id="@+id/editText2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBaseline="@+id/textView2"android:layout_alignBottom="@+id/textView2"android:layout_alignLeft="@+id/editText1"android:layout_alignRight="@+id/editText1"android:ems="10"android:inputType="textPassword" /><EditTextandroid:id="@+id/editText1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBaseline="@+id/textView1"android:layout_alignBottom="@+id/textView1"android:layout_toRightOf="@+id/textView1"android:ems="10" /><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:text="用户名:" /></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 Dialog
APIs. 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形式的出现效果!
Android开发之自定义dialog的实现(源代码分享)相关推荐
- Android开发之自定义Dialog二次打开报错问题解决
之前自定义了一个AlertDialog对话框,第一次点击时正常,但第二次调用时会出现错误:java.lang.IllegalStateException: The specified child al ...
- android 自定义dialog 定时关闭,Android编程实现自定义Dialog的大小自动控制方法示例...
本文实例讲述了Android编程实现自定义Dialog的大小自动控制方法.分享给大家供大家参考,具体如下: Android应用开发中,无论是出于功能还是增加用户体验,弹出对话框(Dialog)进行一些 ...
- 《Android开发卷——自定义日期选择器(三)》
继 <Android开发卷--自定义日期选择器(一)>:http://blog.csdn.net/chillax_li/article/details/19047 ...
- 《Android开发卷——自定义日期选择器(二)》
(小米手机) (中兴手机) 在上一篇中,我介绍了一般公司都会自定义时间日期选择器,并结合自己所做的项目给大家参考. 工作实录之<Android开发卷--自定义日期选择器(一)>链接:htt ...
- android自定义横竖双向滚动,Android开发实现自定义水平滚动的容器示例
Android开发实现自定义水平滚动的容器示例 发布时间:2020-09-12 01:25:56 来源:脚本之家 阅读:71 作者:CharlinGod 本文实例讲述了Android开发实现自定义水平 ...
- android实现新闻内容显示功能,Android开发实现自定义新闻加载页面功能实例
本文实例讲述了Android开发实现自定义新闻加载页面功能.分享给大家供大家参考,具体如下: 一.概述: 1.效果演示: 2.说明:在新闻页面刚加载的时候,一般会出现五种状态 未知状态(STATE_U ...
- 【Android开发】自定义圆角button样式
[Android开发]自定义圆角button样式 结果图 步骤 在res/drawable中新建一个drawable文件,自定义命名为btu.xml; 在btu.xml中设置圆角大小和填充颜色,代码如 ...
- Android开发,自定义View的学习合集
转载自:http://blog.csdn.net/u011507982/article/details/51199644 自定义控件学习 https://github.com/GcsSloop/An ...
- 【Android开发】自定义ListView,使用通用适配器,并实现ListView上的每一项和每一项上的按钮等控件同时监听
ListView在Android开发中是比较常用的系统组件,但是有时候我们除了需要做ListView上每一行的点击监听事件之外,如果每一行上还有其他需要监听的控件例如Button.CheckBox等, ...
最新文章
- Linux终端运行fasterrcnn,对yolo与fasterrcnn anchors的理解
- 干货|python基础知识总结
- Pydoc 本地 HTML 形式查看
- java通过ftp上传文件到服务器_java-通过FTP将文件传输到服务器,但结果以零...
- 以太坊智能合约安全 Dasp Top10
- Prim算法的3个版本
- Quartz集成到 Spring
- [工具类]文件或文件夹xx已存在,则重命名为xx(n)(2)
- mysql -f --force_MySQL force Index 强制索引概述
- vba中split用法
- Untiy相机实现游戏透视效果
- 学生a3制图标题栏尺寸手绘_AutoCAD教程之绘制图框和标题栏
- 无监督学习——非负矩阵分解(NMF)
- 《SEM长尾搜索营销策略解密》一一2.8 长尾虽好,但核心不可或缺
- 程序员壁纸推荐,高清无码无水印地址已奉上!
- 机器学习特征值处理方案
- java 兔子生兔子
- pandas读取带中文的csv
- 常见排序算法(C语言实现)
- D:\ANdriod\emulator\qemu\windows-x86_64\qemu-system-i386.exe: Could not open 'C:\Users\榛刓.android\a