Skip to content

Commit

Permalink
add some custom dialog.
Browse files Browse the repository at this point in the history
  • Loading branch information
leguang committed Dec 6, 2017
1 parent ae86a72 commit 8fadb58
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void onClick(View v) {
});
}
})
.setDimAmount(0.3f)//设置window的暗度。
.setDimAmount(0.3F)//设置window的暗度。
.setGravity(Gravity.TOP)//位置有多种选择。
.setAnimStyle(R.style.SlideAnimation)//进入和退出动画。
.show();//显示。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
Expand Down Expand Up @@ -183,6 +181,7 @@ public void onClick(View view) {
}
})
.setDimAmount(0.3f)
.setMargin(60)
.show(getSupportFragmentManager());
dialog.setCancelable(false);
break;
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/confirm_layout.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="40dp"
android:background="@drawable/white_round_corner_bg"
android:gravity="center_horizontal"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cn.itsite.adialog.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
Expand All @@ -25,7 +23,7 @@ public class BaseDialog extends AppCompatDialog {
private static final String LAYOUT = "layout_id";
private int width;//宽度
private int height;//高度
private float dimAmount = 0.5F;//灰度深浅
private float dimAmount = 0.5f;//灰度深浅
private int gravity;//是否底部显示
@StyleRes
private int animStyle;
Expand Down Expand Up @@ -86,7 +84,7 @@ private void initWindow() {
Window window = getWindow();
if (window != null) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setBackgroundDrawableResource(android.R.color.transparent);
//设置dialog进入、退出的动画
window.setWindowAnimations(animStyle);
WindowManager.LayoutParams lp = window.getAttributes();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cn.itsite.adialog.dialogfragment;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
Expand All @@ -19,18 +17,21 @@

import cn.itsite.adialog.ADialogListener;
import cn.itsite.adialog.BaseViewHolder;
import cn.itsite.adialog.R;
import cn.itsite.adialog.Utils;

public class BaseDialogFragment extends AppCompatDialogFragment {
private static final String MARGIN = "margin";
private static final String WIDTH = "width";
private static final String HEIGHT = "height";
private static final String DIM = "dim_amount";
private static final String GRAVITY = "gravity";
private static final String ANIM = "anim_style";
private static final String LAYOUT = "layout_id";
private int margin;//左右边距
private int width;//宽度
private int height;//高度
private float dimAmount = 0.5F;//灰度深浅
private float dimAmount = 0.5f;//灰度深浅
private int gravity;//是否底部显示
@StyleRes
private int animStyle;
Expand All @@ -42,10 +43,11 @@ public class BaseDialogFragment extends AppCompatDialogFragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_NoTitleBar_Fullscreen);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.BaseDialog);
layoutId = getLayoutId();
//恢复保存的数据
if (savedInstanceState != null) {
margin = savedInstanceState.getInt(MARGIN);
width = savedInstanceState.getInt(WIDTH);
height = savedInstanceState.getInt(HEIGHT);
dimAmount = savedInstanceState.getFloat(DIM);
Expand Down Expand Up @@ -95,6 +97,7 @@ public void onStart() {
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(MARGIN, margin);
outState.putInt(WIDTH, width);
outState.putInt(HEIGHT, height);
outState.putFloat(DIM, dimAmount);
Expand All @@ -109,12 +112,12 @@ private void initWindow() {
//设置dialog进入、退出的动画
window.setWindowAnimations(animStyle);
WindowManager.LayoutParams lp = window.getAttributes();
//调节灰色背景透明度[0-1],默认0.5F
//调节灰色背景透明度[0-1],默认0.5f
lp.dimAmount = dimAmount;
lp.gravity = gravity;
//设置dialog宽度
if (width == 0) {
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.width = Utils.getScreenWidth(getContext()) - 2 * Utils.dp2px(getContext(), margin);
} else {
lp.width = Utils.dp2px(getContext(), width);
}
Expand Down Expand Up @@ -148,6 +151,11 @@ public BaseDialogFragment setDialog(Dialog dialog) {
return this;
}

public BaseDialogFragment setMargin(int margin) {
this.margin = margin;
return this;
}

public BaseDialogFragment setWidth(int width) {
this.width = width;
return this;
Expand Down Expand Up @@ -182,4 +190,4 @@ public BaseDialogFragment setConvertListener(ADialogListener.OnDialogFragmentCon
this.mConvertListener = listener;
return this;
}
}
}

0 comments on commit 8fadb58

Please sign in to comment.