Android如何自定义view仿iOS弹出框效果
这篇文章将为大家详细讲解有关Android如何自定义view仿iOS弹出框效果,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
站在用户的角度思考问题,与客户深入沟通,找到广平网站设计与广平网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站建设、做网站、企业官网、英文网站、手机端网站、网站推广、域名注册、网站空间、企业邮箱。业务覆盖广平地区。
1.layout布局文件
view_actionsheet.xml
view_alertdialog.xml
2.style.xml文件
3.color.xml文件
4.dimen.xml文件
5.anim动画
actionsheet_dialog_in.xml
actionsheet_dialog_out.xml
6.drawable文件夹的诸多资源
资源下载来源
7.底部弹出框
import android.app.Dialog;import android.content.Context;import android.graphics.Color;import android.view.Display;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.view.WindowManager;import android.widget.LinearLayout;import android.widget.ScrollView;import android.widget.TextView;import java.util.ArrayList;import java.util.List;/** * Author:AND * Time:2018/3/16. * Email:2911743255@qq.com * Description: * Detail: */public class ActionSheetDialog { private Context context; private Dialog dialog; private TextView txt_title; private TextView txt_cancel; private LinearLayout lLayout_content; private ScrollView sLayout_content; private boolean showTitle = false; private List
8.中间弹出框
import android.app.Dialog;import android.content.Context;import android.view.Display;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.WindowManager;import android.widget.Button;import android.widget.FrameLayout;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;/** * Author:AND * Time:2018/3/16. * Email:2911743255@qq.com * Description: * Detail: */public class AlertDialog { private Context context; private Dialog dialog; private LinearLayout lLayout_bg; private TextView txt_title; private TextView txt_msg; private Button btn_neg; private Button btn_pos; private ImageView img_line; private Display display; private boolean showTitle = false; private boolean showMsg = false; private boolean showPosBtn = false; private boolean showNegBtn = false; public AlertDialog(Context context) { this.context = context; WindowManager windowManager = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); display = windowManager.getDefaultDisplay(); } public AlertDialog builder() { // 获取Dialog布局 View view = LayoutInflater.from(context).inflate( R.layout.view_alertdialog, null); // 获取自定义Dialog布局中的控件 lLayout_bg = (LinearLayout) view.findViewById(R.id.lLayout_bg); txt_title = (TextView) view.findViewById(R.id.txt_title); txt_title.setVisibility(View.GONE); txt_msg = (TextView) view.findViewById(R.id.txt_msg); txt_msg.setVisibility(View.GONE); btn_neg = (Button) view.findViewById(R.id.btn_neg); btn_neg.setVisibility(View.GONE); btn_pos = (Button) view.findViewById(R.id.btn_pos); btn_pos.setVisibility(View.GONE); img_line = (ImageView) view.findViewById(R.id.img_line); img_line.setVisibility(View.GONE); // 定义Dialog布局和参数 dialog = new Dialog(context, R.style.AlertDialogStyle); dialog.setContentView(view); // 调整dialog背景大小 lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display .getWidth() * 0.85), ViewGroup.LayoutParams.WRAP_CONTENT)); return this; } public AlertDialog setTitle(String title) { showTitle = true; if ("".equals(title)) { txt_title.setText("标题"); } else { txt_title.setText(title); } return this; } public AlertDialog setMsg(String msg) { showMsg = true; if ("".equals(msg)) { txt_msg.setText("内容"); } else { txt_msg.setText(msg); } return this; } public AlertDialog setCancelable(boolean cancel) { dialog.setCancelable(cancel); return this; } public AlertDialog setPositiveButton(String text, final View.OnClickListener listener) { showPosBtn = true; if ("".equals(text)) { btn_pos.setText("确定"); } else { btn_pos.setText(text); } btn_pos.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { listener.onClick(v); dialog.dismiss(); } }); return this; } public AlertDialog setNegativeButton(String text, final View.OnClickListener listener) { showNegBtn = true; if ("".equals(text)) { btn_neg.setText("取消"); } else { btn_neg.setText(text); } btn_neg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { listener.onClick(v); dialog.dismiss(); } }); return this; } private void setLayout() { if (!showTitle && !showMsg) { txt_title.setText("提示"); txt_title.setVisibility(View.VISIBLE); } if (showTitle) { txt_title.setVisibility(View.VISIBLE); } if (showMsg) { txt_msg.setVisibility(View.VISIBLE); } if (!showPosBtn && !showNegBtn) { btn_pos.setText("确定"); btn_pos.setVisibility(View.VISIBLE); btn_pos.setBackgroundResource(R.drawable.alertdialog_single_selector); btn_pos.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); } if (showPosBtn && showNegBtn) { btn_pos.setVisibility(View.VISIBLE); btn_pos.setBackgroundResource(R.drawable.alertdialog_right_selector); btn_neg.setVisibility(View.VISIBLE); btn_neg.setBackgroundResource(R.drawable.alertdialog_left_selector); img_line.setVisibility(View.VISIBLE); } if (showPosBtn && !showNegBtn) { btn_pos.setVisibility(View.VISIBLE); btn_pos.setBackgroundResource(R.drawable.alertdialog_single_selector); } if (!showPosBtn && showNegBtn) { btn_neg.setVisibility(View.VISIBLE); btn_neg.setBackgroundResource(R.drawable.alertdialog_single_selector); } } public void show() { setLayout(); dialog.show(); }}
9.具体使用
activity调用
@Override public void onClick(View v) { switch (v.getId()) { case R.id.click: // TODO 18/03/16 new ActionSheetDialog(this) .builder() .setTitle("清空消息列表后,聊天记录依然保留,确定要清空消息列表?") .setCancelable(false) .setCanceledOnTouchOutside(false) .addSheetItem("清空消息列表", ActionSheetDialog.SheetItemColor.Red , new ActionSheetDialog.OnSheetItemClickListener() { @Override public void onClick(int which) { } }).show(); break; case R.id.iamge:// TODO 18/03/16 new ActionSheetDialog(this) .builder() .setCancelable(false) .setCanceledOnTouchOutside(false) .addSheetItem("发送给好友", ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener() { @Override public void onClick(int which) { } }) .addSheetItem("转载到空间相册", ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener() { @Override public void onClick(int which) { } }) .addSheetItem("上传到群相册", ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener() { @Override public void onClick(int which) { } }) .addSheetItem("保存到手机", ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener() { @Override public void onClick(int which) { } }) .addSheetItem("收藏", ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener() { @Override public void onClick(int which) { } }) .addSheetItem("查看聊天图片", ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener() { @Override public void onClick(int which) { } }).show(); break; case R.id.ctrol:// TODO 18/03/16 new ActionSheetDialog(this) .builder() .setTitle("请选择操作") .setCancelable(false) .setCanceledOnTouchOutside(false) .addSheetItem("条目一", ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener() { @Override public void onClick(int which) { } }) .addSheetItem("条目二", ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener() { @Override public void onClick(int which) { } }).show(); break; case R.id.login:// TODO 18/03/16 new AlertDialog(this).builder().setTitle("退出当前账号") .setMsg("再连续登陆15天,就可变身为QQ达人。退出QQ可能会使你现有记录归零,确定退出?") .setPositiveButton("确认退出", new View.OnClickListener() { @Override public void onClick(View v) { } }).setNegativeButton("取消", new View.OnClickListener() { @Override public void onClick(View v) { } }).show(); break; case R.id.msg:// TODO 18/03/16 new AlertDialog(this).builder() .setMsg("你现在无法接收到新消息提醒。请到系统-设置-通知中开启消息提醒") .setNegativeButton("确定", new View.OnClickListener() { @Override public void onClick(View v) { } }).show(); break; default: break; } }
关于“Android如何自定义view仿iOS弹出框效果”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
分享标题:Android如何自定义view仿iOS弹出框效果
文章出自:http://azwzsj.com/article/piiiie.html