BaseDialog的封装
来源:互联网 发布:c# 数据库接口开发 编辑:程序博客网 时间:2024/06/06 09:40
当我们的项目越来越大是,我们工程里会有很多的东西都基本上是重复编写的,这时候我们要想到的是封装。
这里为大家提供一个BaseEnsureDialog
/** * Created by lili on 2017/5/18. * 对话框基类 * 1.要改变对话框的显示位置,请重写getGravity()方法,默认显示在屏幕中间 Gravity.CENTER_HORIZONTAL * 2.改变点击对话框外部是否消失,请重写isTouchCancle()方法,默认不能消失false * 3.改变对话框的宽度,请重写getMarginLeftRight()方法, 默认宽度距离屏幕两边的距离是JeaCommon.DIALOG_MARGIN_LEFTRIGHT * 4.具体逻辑处理 在子类构造方法中写入。 * * 具体使用可移步AcceptDialog 和LessStudyCoinDialog */public abstract class BaseEnsureDialog { protected Dialog mDialog; protected Context mContext; private int mMargin = JeaCommon.DIALOG_MARGIN_LEFTRIGHT; public BaseEnsureDialog( Context context){ this.mContext = context; mDialog = new Dialog(context, R.style.DailyDiscountTypeDialogStyle); mDialog.setCanceledOnTouchOutside(isTouchCancle()); mDialog.setContentView(getLayoutId()); Window window = mDialog.getWindow(); window.setGravity(getGravity()); Rect rect = new Rect(); View view1 = window.getDecorView(); view1.getWindowVisibleDisplayFrame(rect); WindowManager.LayoutParams windowparams = mDialog.getWindow().getAttributes(); windowparams.width = ScreenUtils.getScreenWidth(mContext)- getMarginLeftRight(); window.setAttributes( windowparams); } public abstract int getLayoutId(); /** * 默认显示在屏幕中间 * @return */ public int getGravity(){ return Gravity.CENTER_HORIZONTAL; } /** * 点击对话框外是否可以消失,默认不能消失 * @return */ public boolean isTouchCancle(){ return false; } /** * 设置dialog布局距离左右的距离,默认是JeaCommon.DIALOG_MARGIN_LEFTRIGHT :100dp * @param */ public int getMarginLeftRight(){ return mMargin; } public void show(){ mDialog.show(); } public void dismiss(){ mDialog.dismiss(); }}
/** * Created by lili on 2017/5/18. * */public class LessStudyCoinDialog extends BaseEnsureDialog{ private TextView mTextEnsure,mTextCancle,mTextValue; private int mRestGolds; public LessStudyCoinDialog(Context context,int glods) { super(context); mRestGolds = glods; initView(); } public void initView() { mTextCancle = (TextView) mDialog.findViewById(R.id.tv_cancle); mTextEnsure = (TextView) mDialog.findViewById(R.id.tv_recharge); mTextValue = (TextView) mDialog.findViewById(R.id.tv_dialog_coins_value); mTextValue.setText("悬赏(可用学金币"+mRestGolds+")"); } @Override public int getLayoutId() { return R.layout.dialog_study_lesscoin; } /** * 设置确定监听 * @param listener */ public void setRechargeListener(View.OnClickListener listener){ mTextEnsure.setOnClickListener(listener); } /** * 设置取消监听 * @param listener */ public void setCancleListener(View.OnClickListener listener){ mTextCancle.setOnClickListener(listener); } /** * 复写父类方法,改变对话框距离左右距离 * @return */ @Override public int getMarginLeftRight() { return 200; }}
dialog_study_lesscoin.xml
<?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:background="@drawable/shape_small_white_grey" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="学金币不足" android:layout_marginTop="25dp" android:textSize="16sp" android:textColor="@color/text_7" /> <TextView android:id="@+id/tv_dialog_coins_value" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="可用学金币为0" android:layout_marginTop="15dp" android:layout_gravity="center_horizontal" android:textColor="@color/c_study_coins" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginTop="24dp" android:background="@color/dingdanchuli" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/tv_recharge" android:layout_width="0dp" android:layout_weight="1" android:gravity="center" android:paddingTop="13dp" android:paddingBottom="13dp" android:paddingLeft="40dp" android:paddingRight="40dp" android:layout_height="wrap_content" android:textColor="@color/tab_main_text_orange" android:text="充值" /> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="@color/dingdanchuli" /> <TextView android:id="@+id/tv_cancle" android:layout_width="0dp" android:layout_weight="1" android:gravity="center" android:paddingTop="13dp" android:paddingBottom="13dp" android:paddingLeft="40dp" android:paddingRight="40dp" android:layout_height="wrap_content" android:textColor="@color/c_tv_cancle" android:text="取消" /> </LinearLayout></LinearLayout>
shape_small_white_grey.xml是个圆角的形状。
这样一个BaseEnsureDialog就可以封装成功。要注意一点就是,只需要在对话布局的最外层使用圆角形就行了,如果内部也使用形状,可能会导致圆角效果和你想象的不一样。
阅读全文
2 0
- BaseDialog的封装
- 14、BaseDialog的界面
- 15、BaseDialog的代码
- BaseDialog
- 自定义BaseDialog,继承BaseDialog实现自己想要的功能。
- Unity客户端架构-BaseDialog
- 安卓自定义Dialog之BaseDialog
- 封装机制封装的概念
- 封装:树形节点的封装
- 项目基本配置集合:BaseDialog,BaseActivity,TitleBar,沉浸式集合。
- Android 下Build模式初使用(BaseDialog)
- 简单的jdbc封装+servlet的封装
- 工具类的封装-sharePreference的封装
- 封装性是指对外部的封装
- 强大的封装标签封装类
- 什么是封装,封装类的使用!
- java的封装(如何封装类)
- alertController的封装(在TYAlertController基础上封装)
- 5.16特长生模拟题 遭遇战
- UVA-10590 正整数拆分
- 制作Centos usb 安装盘 for Mac
- GCD常用方法
- AsyncTask使用总结
- BaseDialog的封装
- 公开密钥加密技术(公钥和私钥)
- 轻量级的.NET对象查找服务和AOP开发框架Netop.Core源码解说(2)--配置
- vs编译器一些常见错误
- js中字符串常用截取函数
- (47)按键绑定编辑器
- window.onload用法详解
- Zabbix-3.04 添加监控主机
- 383. Ransom Note