当我们的项目越来越大是,我们工程里会有很多的东西都基本上是重复编写的,这时候我们要想到的是封装。
这里为大家提供一个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就可以封装成功。要注意一点就是,只需要在对话布局的最外层使用圆角形就行了,如果内部也使用形状,可能会导致圆角效果和你想象的不一样。