public class BottomDialog extends Dialog {
private List<String> dataSource;
private ItemClick click;
private boolean isaddCancelItem;
public BottomDialog(@NonNull Context context) {
super(context, R.style.XAlert);//去除默认主题,不去除会有黑色框
}
public BottomDialog(@NonNull Context context, int themeResId) {
super(context, R.style.XAlert);
}
public BottomDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setWindowAttributes();
getWindow().getDecorView().setPadding(0, 0, 0, 0);
View contentView = createDialogView();
onViewCreated(contentView);
if(isaddCancelItem){addCancelItem(contentView);}
setContentView(contentView);
}
private void setWindowAttributes() {
Window window = getWindow();
requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams attributes = window.getAttributes();
attributes.gravity = Gravity.BOTTOM;
attributes.width = getContext().getResources().getDisplayMetrics().widthPixels;
window.setAttributes(attributes);
}
int i=0;
private void onViewCreated(View contentView) {
LinearLayoutCompat.LayoutParams params=new LinearLayoutCompat.LayoutParams(getContext().getResources().getDisplayMetrics().widthPixels,LinearLayout.LayoutParams.WRAP_CONTENT);
if(dataSource!=null){
for(i=0;i<dataSource.size();i++){
TextView textView=new TextView(getContext());
textView.setLayoutParams(params);
textView.setText(dataSource.get(i));
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,18);
textView.setTextColor(Color.parseColor("#ff3b30"));
textView.setGravity(Gravity.CENTER);
textView.setBackgroundColor(Color.WHITE);
textView.setPadding(0,dp2px(10),0,dp2px(10));
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(click!=null){click.onclick(i);}
dismiss();
}
});
((ViewGroup) contentView).addView(textView);
if(dataSource.size()-1>=1){
View view=new View(getContext());
view.setBackgroundColor(Color.parseColor("#dfe2e8"));
view.setLayoutParams(new LinearLayoutCompat.LayoutParams(getContext().getResources().getDisplayMetrics().widthPixels,dp2px(5)));
((ViewGroup) contentView).addView(view);
}
}
}
}
public void addCancelItem(View contentView){
View view=new View(getContext());
view.setBackgroundColor(Color.parseColor("#dfe2e8"));
view.setLayoutParams(new LinearLayoutCompat.LayoutParams(getContext().getResources().getDisplayMetrics().widthPixels,dp2px(5)));
((ViewGroup) contentView).addView(view);
TextView cancelview=new TextView(getContext());
cancelview.setLayoutParams(new LinearLayoutCompat.LayoutParams(getContext().getResources().getDisplayMetrics().widthPixels,LinearLayout.LayoutParams.WRAP_CONTENT));
cancelview.setText("取消");
cancelview.setTextSize(TypedValue.COMPLEX_UNIT_SP,18);
cancelview.setTextColor(Color.parseColor("#505864"));
cancelview.setBackgroundColor(Color.WHITE);
cancelview.setGravity(Gravity.CENTER);
cancelview.setPadding(0,dp2px(10),0,dp2px(10));
cancelview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
((ViewGroup) contentView).addView(cancelview);
}
private View createDialogView(){
LinearLayout dialogView=new LinearLayout(getContext());
dialogView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT));
dialogView.setBackgroundColor(Color.parseColor("#dfe2e8"));
dialogView.setOrientation(LinearLayout.VERTICAL);
return dialogView;
}
public BottomDialog setDataSource(List<String> dataSource){
this.dataSource = dataSource;
return this;
}
public BottomDialog IsaddCancelItem(Boolean isaddCancelItem){
this.isaddCancelItem=isaddCancelItem;
return this;
}
public BottomDialog setClick(ItemClick click) {
this.click = click;
return this;
}
public interface ItemClick{
public void onclick(int position);
}
protected int dp2px(float dp) {
final float scale = getContext().getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}
}
底部Dialog(无布局文件)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...