首先我们需要在资源文件中定义dialog样式;由于系统默认的样式有标题栏,有边框,背景颜色也不符合我们的业务需求,所以我们需要自己定义样式
<style name="ios_bottom_dialog" parent="android:style/Theme.Dialog">
<!--背景颜色及和透明程度-->
<item name="android:windowBackground">@color/transparent</item>
<!--是否去除标题 -->
<item name="android:windowNoTitle">true</item>
<!--是否去除边框-->
<item name="android:windowFrame">@null</item>
<!--是否浮现在activity之上-->
<item name="android:windowIsFloating">true</item>
<!--是否模糊-->
<item name="android:backgroundDimEnabled">true</item>
<!--背景透明度-->
<item name="android:backgroundDimAmount">0.2</item>
</style>
定义一个dialog,基本上以上定义以上几个属性就可以了
第二步我们就要写布局文件了,布局文件可以分成上中下三部分,上下部分很简单,就是两个textView,用来表示标题和cancel按钮;中间为了拓展性,没有定义具体按钮而是用LinearLayout,具体内容可以在代码中设置;
<?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="wrap_content"
android:paddingBottom="10dp"
android:background="@android:color/transparent"
android:alpha="0.9">
<LinearLayout
android:id="@+id/bottom_dialog_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bottom_dialog_selector"
android:orientation="vertical">
<TextView
android:id="@+id/bottom_dialog_title_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:clickable="false"
android:gravity="center"
android:textSize="20dp" />
<View android:id="@+id/bottom_dialog_title_line"
android:layout_width="match_parent"
android:layout_height="0.3dp"
android:background="@color/black_444444" />
<LinearLayout
android:id="@+id/options_ll"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:layout_below="@+id/bottom_dialog_ll"
android:layout_marginTop="8dp"
android:id="@+id/bottom_dialog_cancel_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bottom_dialog_selector"
android:gravity="center"
android:padding="8dp"
android:textColor="@color/text_color_green_013ADF"
android:text="@string/cancel"
android:textSize="20dp" />
</RelativeLayout>
接下来是程序部分,自定义一个BottomDialog类继承Dialog;代码如下
public class BottomDialog extends Dialog {
private static final int DEFAULT_PADDING = 10;
private static final int DEFAULT_TITLE_SIZE = 15;
private static final int DEFAULT_OPTION_SIZE = 20;
private TextView title;
private View title_line;
private LinearLayout options_ll;
private IosBottomDialogDismissListener dismissListener;
public BottomDialog(Context context) {
//设置自定义的style
this(context, R.style.ios_bottom_dialog);
}
public BottomDialog(Context context, int themeResId) {
super(context, themeResId);
setContentView(R.layout.ios_bottom_dialog);
initView();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private void initView() {
title = (TextView) findViewById(R.id.bottom_dialog_title_tv);
title_line = findViewById(R.id.bottom_dialog_title_line);
options_ll = (LinearLayout) findViewById(R.id.options_ll);
findViewById(R.id.bottom_dialog_cancel_tv).setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
BottomDialog.this.dismiss();
}
});
//点击空白区域可以取消dialog
this.setCanceledOnTouchOutside(true);
// 点击back键可以取消dialog
this.setCancelable(true);
Window window = this.getWindow();
//让Dialog显示在屏幕的底部
window.setGravity(Gravity.BOTTOM);
//设置窗口出现和窗口隐藏的动画
window.setWindowAnimations(R.style.ios_bottom_dialog_anim);
//设置BottomDialog的宽高属性
WindowManager.LayoutParams lp = window.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
}
public void setTitle(String text) {
title.setText(text);
}
private void setIosBottomDialogDismissListener(IosBottomDialogDismissListener listener) {
this.dismissListener = listener;
}
public interface OnOptionClickListener {
void onOptionClick();
}
public interface IosBottomDialogDismissListener {
void onDismiss();
}
public static class Builder {
private Paraments p;
private Context context;
public Builder(Context context) {
p = new Paraments();
this.context = context;
}
public Builder setTitle(String title, int color) {
p.title = title;
p.titleColor = color;
return this;
}
public Builder addOption(String option, int color, OnOptionClickListener listener) {
p.options.add(new Option(option, color, listener));
return this;
}
public Builder setDialogDismissListener(IosBottomDialogDismissListener listener) {
p.dismisslistener = listener;
return this;
}
private int dp2px(float dipValue, Context context) {
float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
public BottomDialog create() {
final BottomDialog dialog = new BottomDialog(context);
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1);
if (p.title.isEmpty()) {
//设置标题栏不可见
dialog.title.setVisibility(View.GONE);
dialog.title_line.setVisibility(View.GONE);
} else {
dialog.title.setText(p.title);
dialog.title.setTextColor(p.titleColor);
dialog.title.setTextSize(p.titleSize);
//设置标题栏可见
dialog.title.setVisibility(View.VISIBLE);
dialog.title_line.setVisibility(View.VISIBLE);
}
//设置item项点击之后的效果
if (p.options.size() == 0) {
dialog.options_ll.setVisibility(View.GONE);
} else {
for (int i = 0; i < p.options.size(); i++) {
final Option option = p.options.get(i);
final TextView optionText = new TextView(context);
int padding = dp2px(BottomDialog.DEFAULT_PADDING, context);
optionText.setPadding(padding, padding, padding, padding);
optionText.setText(option.getName());
optionText.setTextSize(p.optionTextSize);
optionText.setGravity(Gravity.CENTER);
optionText.setTextColor(context.getResources().getColor(R.color.text_color_green_013ADF));
optionText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
if (option.getListener() != null) {
option.getListener().onOptionClick();
}
}
});
dialog.options_ll.addView(optionText);
//添加条目之间的分割线
if (i != p.options.size() - 1) {
View divider = new View(context);
divider.setBackgroundResource(R.color.black_444444);
dialog.options_ll.addView(divider, params);
}
//选择到底使用哪个selector文件
if (p.options.size() == 1) {
if (p.title.isEmpty()) {
optionText.setBackgroundResource(R.drawable.bottom_dialog_option13);
} else {
optionText.setBackgroundResource(R.drawable.bottom_dialog_option3);
}
} else if (i == 0) {
if (p.title.isEmpty()) {
optionText.setBackgroundResource(R.drawable.bottom_dialog_option1);
} else {
optionText.setBackgroundResource(R.drawable.bottom_dialog_option2);
}
} else if (i < p.options.size() - 1) {
optionText.setBackgroundResource(R.drawable.bottom_dialog_option2);
} else {
optionText.setBackgroundResource(R.drawable.bottom_dialog_option3);
}
}
}
//对话框dismiss之后的监听
dialog.setIosBottomDialogDismissListener(p.dismisslistener);
return dialog;
}
}
//这个类保存了dialog的众多参数
private static class Paraments {
int titleSize;
int optionTextSize;
String title;
int titleColor;
boolean cancelable;
List<Option> options;
BottomDialog.IosBottomDialogDismissListener dismisslistener;
Paraments() {
title = "";
titleColor = R.color.gray_title;
cancelable = true;
options = new ArrayList<>();
titleSize = DEFAULT_TITLE_SIZE;
optionTextSize = DEFAULT_OPTION_SIZE;
}
}
private static class Option {
private String name;
private int color;
private BottomDialog.OnOptionClickListener listener;
public Option() {
}
Option(String name, int color, BottomDialog.OnOptionClickListener listener) {
this.name = name;
this.color = color;
this.listener = listener;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
BottomDialog.OnOptionClickListener getListener() {
return listener;
}
public void setListener(BottomDialog.OnOptionClickListener listener) {
this.listener = listener;
}
}
}
在构造方法中设置style、布局,关键代码都有注释;然后就是设置出现于隐藏动画,代码如下
<!--dialog进入动画-->
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromYDelta="100%p"
android:toYDelta="0" />
<!--dialog退出动画-->
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromYDelta="0"
android:toYDelta="100%p" />
写好动画后,在style文件中引用,代码如下
<!-- 弹窗动画 由下至上 -->
<style name="ios_bottom_dialog_anim" parent="@android:style/Animation.Dialog">
<item name="@android:windowEnterAnimation">@anim/ios_dialog_enter</item>
<!-- 进入时的动画 -->
<item name="@android:windowExitAnimation">@anim/ios_dialog_exit</item>
<!-- 退出时的动画 -->
</style>
这样我们的dialog就完成了,程序中用到的一些资源文件没有贴出来,大家可以自己定义。
网友评论