一、UI显示
日常生活中,我们经常会看到应用中那些提示框,有在正中的,有在底部的,这篇文章我来学习一下这两类提示框UI,主要是dialog和popuwindow。
首先上图:
1、dialog

2、popuwindow

二、代码的编写(dialog)
dialog
1、首先我们来构思一下所要显示的样式,基本上是分三个部分,①标题②内容③取消确认键。这里我使用的是layout_weight,是为了控制显示比例,易于适配屏幕。

<?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"><LinearLayout
        android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextView
            android:id="@+id/view_custom_alter_dialog_title"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="center"android:text="@string/view_load_gif_dialog_title"android:textSize="@dimen/view_load_gif_title_text_size" /><TextView
            android:id="@+id/view_custom_alter_dialog_content"android:layout_width="match_parent"android:layout_height="0dp"android:text="@string/view_load_gif_dialog_message"android:layout_weight="2"android:gravity="center"android:layout_marginLeft="@dimen/view_load_gif_content_margin_left_right"android:layout_marginRight="@dimen/view_load_gif_content_margin_left_right"android:layout_marginBottom="@dimen/view_load_gif_content_margin_bottom"android:textSize="@dimen/view_load_gif_content_text_size"android:lineSpacingExtra="@dimen/view_load_gif_line_space_extra"/><View
            android:layout_width="match_parent"android:layout_height="1dp"android:background="@color/activity_main_bottom_controller_bg" /><LinearLayout
            android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1.5"android:orientation="horizontal"><TextView
                android:id="@+id/view_custom_alter_dialog_cancel"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:textColor="@color/app_main_color"android:text="@string/view_random_code_dialog_cancel"android:textSize="@dimen/view_load_gif_cancel_sure_textsize" /><View
                android:layout_width="1dp"android:layout_height="match_parent"android:background="@color/activity_main_bottom_controller_bg" /><TextView
                android:id="@+id/view_custom_alter_dialog_sure"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:textColor="@color/app_main_color"android:text="@string/view_random_code_dialog_sure"android:textSize="@dimen/view_load_gif_cancel_sure_textsize" /></LinearLayout></LinearLayout>
</RelativeLayout>

2、这里我们就已经有一个基本的布局了,然后是自定义这个布局。创建一个类来继承Dialog,实现重写构造函数,然后绑定一些控件。
这里我使用的是按照获取到的手机的屏幕,来控制这个dialog的显示比例,这样可以适配的更加方便。

public class CustomAlertDialog extends Dialog {protected View mView;//uiprotected TextView mTextTitle,mTextContent,mTextCancel,mTextSure;public CustomAlertDialog(Context context) {this(context,0);}public CustomAlertDialog(Context context, int themeResId) {super(context, themeResId);mView = LayoutInflater.from(context).inflate(R.layout.view_custom_alert_dialog,null);setContentView(mView);initView();//setting sizeWindow dialogWindow = this.getWindow();WindowManager m = getWindow().getWindowManager();Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值//手机横竖屏时候if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){p.height = (int) (d.getHeight() * 0.25); // 高度设置为屏幕的p.width = (int) (d.getWidth() * 0.7); // 宽度设置为屏幕的}else if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){p.height = (int) (d.getHeight() * 0.3); // 高度设置为屏幕的p.width = (int) (d.getWidth() * 0.4); // 宽度设置为屏幕的}dialogWindow.setAttributes(p);}private void initView() {try{mTextTitle = (TextView) mView.findViewById(R.id.view_custom_alter_dialog_title);mTextContent = (TextView) mView.findViewById(R.id.view_custom_alter_dialog_content);mTextCancel = (TextView) mView.findViewById(R.id.view_custom_alter_dialog_cancel);mTextSure = (TextView) mView.findViewById(R.id.view_custom_alter_dialog_sure);}catch(Exception _e){ExceptionHandler.onException(_e);}}public void setText(String _title,String _content,String _cancel,String _sure){try{if (null != _title)mTextTitle.setText(_title);if (null != _content)mTextContent.setText(_content);if (null != _cancel)mTextCancel.setText(_cancel);if (null != _sure)mTextSure.setText(_sure);}catch(Exception _e){ExceptionHandler.onException(_e);}}public void setLeftOnclick(View.OnClickListener _onclick){try{mTextCancel.setOnClickListener(_onclick);}catch(Exception _e){ExceptionHandler.onException(_e);}}public void setRightOnclick(View.OnClickListener _onclick){try{mTextSure.setOnClickListener(_onclick);}catch(Exception _e){ExceptionHandler.onException(_e);}}}

3、然后在代码中使用这个控件,这里我们注意一下,还要传入hemeResId的值,因为如果不传入,效果就不会那么好。

public void reDownloadResourceDialog(){try{final CustomAlertDialog _customAlterDialog = new CustomAlertDialog(this,R.style.random_code_dialog);_customAlterDialog.show();_customAlterDialog.setLeftOnclick(new View.OnClickListener() {@Overridepublic void onClick(View v) {_customAlterDialog.dismiss();}});_customAlterDialog.setRightOnclick(new View.OnClickListener() {@Overridepublic void onClick(View v) {_customAlterDialog.dismiss();ActivityActionList.this.reDownloadResources();}});}catch(Exception _e){ExceptionHandler.onException(_e);}}

4、使用themeResId的样式,styles.xml文件中

<!--view_random_dialog--><style name="random_code_dialog" parent="@android:style/Theme.Dialog"><item name="android:background">@drawable/dialog_random_code_shape</item><item name="android:windowIsFloating">true</item><item name="android:windowIsTranslucent">false</item><item name="android:windowNoTitle">true</item><item name="android:windowContentOverlay">@null</item><item name="android:windowBackground">@android:color/transparent</item></style><!--view_random_dialog end-->

外加background的样式,主要是周围显示的圆角,dialog_random_code_shape.xml文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><corners android:radius="10dp" /><solid android:color="@color/common_white" />
</shape>

这里dialog的自定义先告一段落。

三、代码的编写(popuwindow)
popuwindow
1、我们也还是先定义一个布局

<?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="wrap_content"android:orientation="vertical"android:padding="@dimen/activity_exercise_plan_xml_padding"><LinearLayout
        android:layout_width="match_parent"android:layout_height="@dimen/activity_exercise_plan_xml_height"android:orientation="vertical"><LinearLayout
            android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="@drawable/plan_more_show_shape"android:orientation="vertical"><TextView
                android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:text="@string/activity_quit_plan_xml_show"android:textSize="@dimen/activity_exercise_plan_xml_show_sp" /></LinearLayout><View
            android:layout_width="match_parent"android:layout_height="@dimen/activity_exercise_plan_xml_splitor"android:background="@color/activity_main_bottom_controller_bg" /><LinearLayout
            android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1.5"android:background="@drawable/plan_more_quit_shape"android:orientation="vertical"><TextView
                android:id="@+id/exercise_plan_more_quit"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:text="@string/activity_quit_plan_xml_quit"android:textColor="@android:color/holo_red_light"android:textSize="@dimen/activity_exercise_plan_xml_text_sp" /></LinearLayout><Space
            android:layout_width="match_parent"android:layout_height="@dimen/activity_exercise_plan_xml_space" /><LinearLayout
            android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1.5"android:background="@drawable/plan_more_cancel_shape"android:orientation="vertical"><TextView
                android:id="@+id/exercise_plan_more_cancel"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:text="@string/activity_quit_plan_xml_cancel"android:textColor="@android:color/holo_blue_light"android:textSize="@dimen/activity_exercise_plan_xml_text_sp" /></LinearLayout></LinearLayout>
</LinearLayout>

布局的样式:

2、有了布局的样式之后,就开始为布局添加绑定。
① mPopWindowMore.showAtLocation(_view, Gravity.BOTTOM, 0, 0);是设置此popuwindow要显示的位置,这里显示在底部
②setBackgroundAlpha()的方法来使得显示popuwindow的时候,会有变暗的效果,当popuwindow消失的时候,又会恢复。bgAlpha取值为0.0-1.0
③使用mPopWindowMore.setOnDismissListener()来监听popuwindow的消失

public void showPopwindowQuitPlan(View _view) {View _viewPopwindow = null;TextView _quitTextView,_cancelTextView;try {_viewPopwindow = LayoutInflater.from(this).inflate(R.layout.view_exercise_plan_more,null);_quitTextView = (TextView) _viewPopwindow.findViewById(R.id.exercise_plan_more_quit);_cancelTextView = (TextView) _viewPopwindow.findViewById(R.id.exercise_plan_more_cancel);if (null != _viewPopwindow){mPopWindowMore = new PopupWindow(_viewPopwindow,ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);mPopWindowMore.setTouchable(true);mPopWindowMore.showAtLocation(_view, Gravity.BOTTOM, 0, 0);mPopWindowMore.setOnDismissListener(new PopupWindow.OnDismissListener() {@Overridepublic void onDismiss() {setBackgroundAlpha(1f);}});}if (null != _quitTextView){_quitTextView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//确认点击}});}if (null != _cancelTextView){_cancelTextView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//取消点击}});}}catch (Exception _e){ExceptionHandler.onException(_e);}}

设置背景颜色的方法

public void setBackgroundAlpha(float bgAlpha) {WindowManager.LayoutParams lp = getWindow().getAttributes();lp.alpha = bgAlpha; //0.0-1.0getWindow().setAttributes(lp);}

Android常用提示框(dialog和popuwindow)相关推荐

  1. android自定义吐司通知,IOS 仿Android吐司提示框的实例(分享)

    直接上代码 #import @interface ShowToastView : UIView +(void)showToastView:(UIView *)uiview WithMessage:(N ...

  2. 【转】 Android常用实例—Alert Dialog的使用

    Android常用实例-Alert Dialog的使用 AlertDialog的使用很普遍,在应用中当你想要用户做出"是"或"否"或者其它各式各样的选择时,为了 ...

  3. Android常用实例—Alert Dialog的使用

    Android常用实例-Alert Dialog的使用 AlertDialog的使用很普遍,在应用中当你想要用户做出"是"或"否"或者其它各式各样的选择时,为了 ...

  4. Android消息提示框及CheckBox组件

    目录 一.Toast(消息提示框) 1.创建显示普通文本的Toast 2.创建显示带图片的Toast 二.CheckBox组件 1.在CheckBox在XML文件中的基本语法 2.CheckBox 选 ...

  5. 详解-Android各种提示框

    首先我们来看看效果图 1.简单的消息提示框 // 简单消息提示框 private void showExitDialog01(){ new AlertDialog.Builder(this) .set ...

  6. Android消息提示框

    Toast(吐丝框) 1.1 Toast是Android中的一种简易的消息提示框 1.2 使用这个类的最简单的方法是调用静态方法构造您所需要的一切,并返回一个新的Toast对象. Toast toas ...

  7. ANDROID各种提示框

    Android开发中我们经常需要用到提示框,以便更好的和用户进行交互 首先我们来看看效果图 实现这个其实很简单,Android的API已经为我们提供了一个提示框类AlertDialog,我们实现的时候 ...

  8. Js自定义提示框(dialog版本)

    问题的来源: 开发的过程中总是要给出各种各样的提示语 ,但是每次都通过手动的形式给后面加提示感觉不舒服  又不想通过服务器控件来做这些事情. 于是就想把提示语和验证整合到一起 套在dialog上面使用 ...

  9. js 自定义类Android吐司提示框

    (function(){     var mouseX = 0;     var mouseY = 0;     //定义一个全局toaslist用来存在新建的吐司     var toastLsit ...

  10. SAP之FIORI(5)-对话框与提示框

    SAP之FIORI(5)-对话框与提示框 Dialog 常用属性: escapeHandler:当点击Escape时的处理方式,默认为关闭对话框 draggable:对话框是否可拖动 horizont ...

最新文章

  1. 用看板工具Leangoo思维导图实现影响地图
  2. java开发人工智能客服_Java开发人工智能客服机器人
  3. python画动态爱心-【Python】五分钟画一条动态心形曲线~
  4. Django死活不跳转的问题
  5. django 给单个文件加log_django配置日志模块
  6. c++超详细基础教程(快速入门)
  7. 运维人员必须熟悉的运维工具汇总
  8. flog和flag_立个flag是什么意思什么梗? 不懂这操作你就out了!
  9. offset 和 零点的一点解释
  10. cad角度怎么画_软件CAD | 直线amp;构造线
  11. 诸葛新增快应用SDK,满足客户更多数据采集方案支持!
  12. 工业级氯化锂2022年全球行业分析报告
  13. 项目经理修炼之道(1) -- 给软件开发建模 .
  14. javascript通过组合实现继承
  15. mysql个人办公使用_Access数据库是给办公人员用的~闲杂人等不要来凑热闹
  16. 如何使用Apple Watch解锁iPhone和Mac?
  17. css零到一中级教程025:CSS 特异性
  18. cesium开发加载shapefile制作的白膜
  19. Jackson 序列化 自定义注解处理Null 值
  20. 数据库第一范式(图解)

热门文章

  1. JM8.6之erc_api.c文件初探
  2. ffmpeg超详细综合教程——摄像头直播
  3. loop设备及losetup命令介绍
  4. Linux signal 那些事儿 (3)
  5. hive sql 13位毫秒时间戳转日期
  6. oracle left join行数,sql – 如何将此LEFT JOIN返回的行数限制为一个?
  7. vs 生成get set_使用EasyCode+Lombok快速生成增删查改的代码
  8. vs2017如何编程python_vs2017怎么编写python代码
  9. yum list 报错linux,centos7下运行yum list 出现如下报错 求教!
  10. drbd mysql mha_浅谈秒级故障切换!用MHA轻松实现MySQL高可用(三)