
一.摘要
d窗通常用于提示用户进行某种 *** 作,比如:点击分享按钮,d窗分享对话框;双击返回按钮,d窗退出对话框;下载文件,提示下载对话框等等,分享对话框/退出对话框/下载对话框,都可以直接使用AlertDialog实现,类似的效果如下图:
二.AlertDialog基础知识
AlertDialog无法直接通过new关键字获取对象,调用方法:new AlertDialog.Builder.create()获取AlertDialog对象,这个时候容易让人疑惑的是:如何设置对话框的属性?比如:对话框标题,对话框消息,对话框按钮等等
设置对话框属性的两种方式
第一种:设置AlertDialog对象属性,具体代码如下:
private voID showDialog() { AlertDialog mDialog = null; mDialog = new AlertDialog.Builder(this).create();; mDialog.setIcon(R.drawable.ic_launcher); mDialog.setTitle("系统提示"); mDialog.setMessage("你确定要退出吗?"); mDialog.setbutton(DialogInterface.button_POSITIVE,"确定",new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialog,int which) { finishMyself(); } }); mDialog.setbutton(DialogInterface.button_NEGATIVE,"取消",int which) { Toast.makeText(MainActivity.this,"再按一次退出程序",(int) touchTime) .show(); } }); mDialog.show(); } 第二种:设置Builder对象属性,具体代码如下:
private voID showDialog() { AlertDialog mDialog = null; Builder mBuilder = new AlertDialog.Builder(this); mBuilder.setIcon(R.drawable.ic_launcher); mBuilder.setTitle("系统提示"); mBuilder.setMessage("你确定要退出吗?"); mBuilder.setPositivebutton("确定",new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialog,int which) { finish(); } }); mBuilder.setNegativebutton("取消",(int) touchTime) .show(); } }); mDialog = mBuilder.create();//创建AlertDialog对象 mDialog.show();//显示创建的AlertDialog } 这两种方式的对话框展示默认属性――对话框水平垂直居中显示,对话框与左右窗体之间有一小段距离,效果图如下:
如何修改默认对话框属性?
如何修改AlertDialog对话框默认属性,然后实现对话框内容宽度布满屏幕,高度根据内容自适应,类似文章开头点击分享按钮,从底部d出d窗的效果。首先创建AlertDialog对话框,然后自定义对话框的布局VIEw,最后设置Window对象属性。
设置Window对象屏幕宽度/高度的三种方式
第一种方式:setLayout()
获得Window对象后,设置Window对象的布局参数,即调用setLayout(int wIDth,int height)方法,wIDth取值:androID.vIEw.WindowManager.LayoutParams.MATCH_PARENT/androID.vIEw.WindowManager.LayoutParams.WRAP_CONTENT,同理height取值:androID.vIEw.WindowManager.LayoutParams.MATCH_PARENT/androID.vIEw.WindowManager.LayoutParams.WRAP_CONTENT,具体代码如下:
VIEw vIEw = getLayoutInflater().inflate(R.layout.popup_dialog,null); AlertDialog mDialog = new AlertDialog.Builder(this).create(); mDialog.show();// 显示创建的AlertDialog,并显示,必须放在Window设置属性之前 /** *设置mDialog窗口属性:MATCH_PARENT/WRAP_CONTENT * */ Window window =mDialog.getwindow(); window.setGravity(Gravity.BottOM); // 此处可以设置dialog显示的位置 window.setLayout(androID.vIEw.WindowManager.LayoutParams.MATCH_PARENT,androID.vIEw.WindowManager.LayoutParams.WRAP_CONTENT);
第二种方式:setAttributes()
获得Window对象后,设置Window对象的属性值,即调用setAttributes(LayoutParams)方法,LayoutParams的wIDth变量取值:androID.vIEw.WindowManager.LayoutParams.MATCH_PARENT/androID.vIEw.WindowManager.LayoutParams.WRAP_CONTENT,同理height变量取值:androID.vIEw.WindowManager.LayoutParams.MATCH_PARENT/androID.vIEw.WindowManager.LayoutParams.WRAP_CONTENT,具体代码如下:
VIEw vIEw = getLayoutInflater().inflate(R.layout.popup_dialog,null); AlertDialog mDialog = new AlertDialog.Builder(this).create(); mDialog.show();// 显示创建的AlertDialog,并显示,必须放在Window设置属性之前 Window window =mDialog.getwindow(); window.setGravity(Gravity.BottOM); // 此处可以设置dialog显示的位置 WindowManager.LayoutParams mParams = window.getAttributes(); mParams.wIDth = androID.vIEw.WindowManager.LayoutParams.MATCH_PARENT; mParams.height = androID.vIEw.WindowManager.LayoutParams.WRAP_CONTENT; window.setGravity(Gravity.BottOM); // 此处可以设置dialog显示的位置 window.setAttributes(mParams);
第三种方式:setLayout()
具体代码如下:
VIEw vIEw = getLayoutInflater().inflate(R.layout.popup_dialog,null); AlertDialog mDialog = new AlertDialog.Builder(this).create(); mDialog.show();// 显示创建的AlertDialog,并显示,必须放在Window设置属性之前 Window window =mDialog.getwindow(); window.setGravity(Gravity.BottOM); // 此处可以设置dialog显示的位置 WindowManager manager = getwindowManager(); display display = manager.getDefaultdisplay(); int wIDth = display.getWIDth();//获取当前屏幕宽度 int height = 300;//自定义高度值,比如:300dp window.setGravity(Gravity.BottOM); // 此处可以设置dialog显示的位置 window.setLayout(wIDth,height);
三.d窗动画基础知识
AndroID的基本动画包括:渐变动画/平移动画/缩放动画/旋转动画/组合动画,点击“分享”按钮,d窗从底部d窗,再次点击d窗消失,设置的动画――平移动画,代码如下:
<?xml version="1.0" enCoding="utf-8"?> <!--enter_dialog_anim.xml,d窗进入动画--> <translate xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:duration="300" androID:fromYDelta="100%"> </translate> <?xml version="1.0" enCoding="utf-8"?> <!--exit_dialog_anim.xml,d窗退出动画--> <translate xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:duration="300" androID:toYDelta="100%" > </translate>
在style.xml文件中添加Window进入和退出分别引用的动画类型,代码如下:
<!-- 分享功能d窗动画 --> <style name="popup_style" parent="androID:Animation"> <item name="@androID:windowEnteranimation">@anim/enter_dialog_anim</item> <item name="@androID:windowExitAnimation">@anim/exit_dialog_anim</item> </style>
在Window属性设置中调用setContentVIEw()指定VIEw对象,同时调用setwindowAnimations()指定添加的动画,代码如下:
window.setContentVIEw(vIEw);//这一步必须指定,否则不出现d窗
window.setwindowAnimations(R.style.popup_style); // 添加动画
四.自定义d窗:MyDialogActivity
自定义MyDialogActivity实现AlertDialog同样的功能,点击“分享按钮”,从窗口底部d出d窗,点击“取消”d窗消息,最终效果和AlertDialog实现的d窗效果一模一样,如下图:
开发步骤:
1.定义布局popup_main.xml。popup_main.xml定义d窗最终展示的样子,可以放置多个平台的分享按钮,比如:微信/微博/空间/人人等,代码如下:
<?xml version="1.0" enCoding="utf-8"?> <!-- 底部d窗布局 --> <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="@color/transparent" androID:orIEntation="vertical" > <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:orIEntation="horizontal" > <TextVIEw androID:ID="@+ID/share_weibo_tv" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_margin="@dimen/share_padding" androID:layout_weight="1" androID:gravity="center_horizontal" androID:text="@string/weibo" /> <TextVIEw androID:ID="@+ID/share_weixin_tv" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_margin="@dimen/share_padding" androID:layout_weight="1" androID:gravity="center_horizontal" androID:text="@string/weixin" /> </linearLayout> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:orIEntation="horizontal" > <TextVIEw androID:ID="@+ID/share_kongjian_tv" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_margin="@dimen/share_padding" androID:layout_weight="1" androID:gravity="center_horizontal" androID:text="@string/kongjian" /> <TextVIEw androID:ID="@+ID/share_qq_tv" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_margin="@dimen/share_padding" androID:layout_weight="1" androID:gravity="center_horizontal" androID:text="@string/qq" /> </linearLayout> <button androID:ID="@+ID/cancel_btn" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_margin="@dimen/activity_vertical_margin" androID:background="@drawable/btn_bg" androID:text="@string/cancel" /> </linearLayout>
2.定义theme样式。theme样式定义在style.xml文件中,在AndroIDManifest.xml文件中的标签的androID:theme=""属性中引用,代码如下:
<!-- MyDialogActivity自定义Threme --> <style name="theme.CustomDialog" parent="@androID:style/theme.Dialog"> <item name="androID:windowNoTitle">true</item> <!-- 设置Title --> <item name="androID:windowBackground">@androID:color/transparent</item> <item name="androID:windowFrame">@null</item> <!-- 设置边框 --> <item name="androID:windowIsTranslucent">true</item> <!-- 设置半透明 --> <item name="androID:windowFullscreen">true</item> <!-- 设置全屏 --> </style> <activity androID:name="MyDialogActivity" androID:theme="@style/theme.CustomDialog"/>
3.实现MyDialogActivity具体功能。
package cn.teachcourse.main; import androID.annotation.Suppresslint; import androID.app.Activity; import androID.graphics.Bitmap; import androID.graphics.BitmapFactory; import androID.graphics.drawable.BitmapDrawable; import androID.graphics.drawable.Drawable; import androID.os.Bundle; import androID.vIEw.Gravity; import androID.vIEw.VIEw; import androID.vIEw.VIEw.OnClickListener; import androID.vIEw.Window; import androID.Widget.button; import androID.Widget.TextVIEw; /* @author postmaster@teachcourse.cn @date 创建于:2016-4-14 */ public class MyDialogActivity extends Activity implements OnClickListener { private VIEw vIEw; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { // Todo auto-generated method stub super.onCreate(savedInstanceState); vIEw = getLayoutInflater().inflate(R.layout.popup_main,null,false); setContentVIEw(vIEw); initVIEw(); } private voID initVIEw() { Window window = getwindow(); window.setLayout(androID.vIEw.VIEwGroup.LayoutParams.MATCH_PARENT,androID.vIEw.VIEwGroup.LayoutParams.WRAP_CONTENT); window.setGravity(Gravity.BottOM); window.setwindowAnimations(R.style.popup_style); // 添加动画 TextVIEw weibo_tv = (TextVIEw) vIEw.findVIEwByID(R.ID.share_weibo_tv); TextVIEw weixin_tv = (TextVIEw) vIEw.findVIEwByID(R.ID.share_weixin_tv); TextVIEw qq_tv = (TextVIEw) vIEw.findVIEwByID(R.ID.share_qq_tv); TextVIEw kongjian_tv = (TextVIEw) vIEw .findVIEwByID(R.ID.share_kongjian_tv); button cancel_btn = (button) vIEw.findVIEwByID(R.ID.cancel_btn); // 添加控件事件 weibo_tv.setonClickListener(this); weixin_tv.setonClickListener(this); qq_tv.setonClickListener(this); kongjian_tv.setonClickListener(this); cancel_btn.setonClickListener(this); // 调整图片的大小/位置 abjustDrawablePos(weibo_tv,R.drawable.share_weibo); abjustDrawablePos(weixin_tv,R.drawable.share_weixin); abjustDrawablePos(kongjian_tv,R.drawable.share_kongjian); abjustDrawablePos(qq_tv,R.drawable.share_qq); } /** * 添加图标和调整位置 * * @param tv * @param draw */ @Suppresslint("ResourceAscolor") private voID abjustDrawablePos(TextVIEw tv,int draw) { Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),draw); mBitmap = centerSquareScaleBitmap(mBitmap,250); Drawable drawable = new BitmapDrawable(mBitmap); drawable.setBounds(0,48,48);// 设置图片的边界 tv.setTextcolor(R.color.Fontcolor); tv.setCompoundDrawables(null,drawable,null);// setCompoundDrawables()和setBounds()方法一起使用 // 添加TextVIEw图标 tv.setCompoundDrawablesWithIntrinsicBounds(null,null); tv.setCompoundDrawablepadding(10);// 设置图片和text之间的间距 if (mBitmap != null) { mBitmap = null; drawable = null; } } /** * * @param bitmap * 原图 * @param edgeLength * 希望得到的正方形部分的边长 * @return 缩放截取正中部分后的位图。 */ public static Bitmap centerSquareScaleBitmap(Bitmap bitmap,int edgeLength) { if (null == bitmap || edgeLength <= 0) { return null; } Bitmap result = bitmap; int wIDthOrg = bitmap.getWIDth(); int heightOrg = bitmap.getHeight(); if (wIDthOrg >= edgeLength && heightOrg >= edgeLength) { // 压缩到一个最小长度是edgeLength的bitmap int longerEdge = (int) (edgeLength * Math.max(wIDthOrg,heightOrg) / Math .min(wIDthOrg,heightOrg)); int scaleDWIDth = wIDthOrg > heightOrg ? longerEdge : edgeLength; int scaledHeight = wIDthOrg > heightOrg ? edgeLength : longerEdge; Bitmap scaledBitmap; try { scaledBitmap = Bitmap.createScaledBitmap(bitmap,scaleDWIDth,scaledHeight,true); } catch (Exception e) { return null; } // 从图中截取正中间的正方形部分。 int xtopleft = (scaleDWIDth - edgeLength) / 2; int ytopleft = (scaledHeight - edgeLength) / 2; try { result = Bitmap.createBitmap(scaledBitmap,xtopleft,ytopleft,edgeLength,edgeLength); scaledBitmap.recycle(); } catch (Exception e) { return null; } } return result; } @OverrIDe public voID onClick(VIEw v) { switch (v.getID()) { /** * 点击分享图标,d出分享界面 */ case R.ID.share_to_btn: break; case R.ID.share_weibo_tv: break; case R.ID.share_weixin_tv: break; case R.ID.share_qq_tv: break; case R.ID.share_kongjian_tv: break; case R.ID.cancel_btn: finish(); break; default: break; } } } 4.d出d窗,调用startActivity(this,MyDialogActivity.class)。
以上就是本文的全部内容,希望对大家学习AndroID软件编程有所帮助。
总结以上是内存溢出为你收集整理的Android AlertDialog实现分享对话框/退出对话框/下载对话框全部内容,希望文章能够帮你解决Android AlertDialog实现分享对话框/退出对话框/下载对话框所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)