扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
这篇文章主要介绍“Android中如何使用ButtomDialog自定义底部弹出框”,在日常操作中,相信很多人在Android中如何使用ButtomDialog自定义底部弹出框问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Android中如何使用ButtomDialog自定义底部弹出框”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
通海网站建设公司创新互联建站,通海网站设计制作,有大型网站制作公司丰富经验。已为通海成百上千提供企业网站建设服务。企业网站搭建\外贸网站制作要多少钱,请找那个售后服务好的通海做网站的公司定做!一 、先来配置自定义控件需要的资源
1.在res文件夹下创建一个anim文件夹并创建两个slide_in_bottom.xml、slide_out_bottom.xml文件,负责弹框进出动画。
2.在style.xml添加阴影和动画样式。
3.在drawable文件夹下创建一个title_background.xml文件,负责给文本内容添加背景。
二、自定义控件的布局
三、自定义控件类
public class ButtomDialog extends Dialog { public ButtomDialog(Context context, int themeResId) { super(context, themeResId); } public static class Params { private final ListmenuList = new ArrayList<>(); private View.OnClickListener cancelListener; private CharSequence menuTitle; private String cancelText; private Context context; } public static class Builder { private boolean canCancel = true; private boolean shadow = true; private final Params p; public Builder(Context context) { p = new Params(); p.context = context; } public Builder setCanCancel(boolean canCancel) { this.canCancel = canCancel; return this; } public Builder setShadow(boolean shadow) { this.shadow = shadow; return this; } public Builder setTitle(CharSequence title) { this.p.menuTitle = title; return this; } public Builder addMenu(String text, View.OnClickListener listener) { BottomMenu bm = new BottomMenu(text, listener); this.p.menuList.add(bm); return this; } public Builder addMenu(int textId, View.OnClickListener listener) { return addMenu(p.context.getString(textId), listener); } public Builder setCancelListener(View.OnClickListener cancelListener) { p.cancelListener = cancelListener; return this; } public Builder setCancelText(int resId) { p.cancelText = p.context.getString(resId); return this; } public Builder setCancelText(String text) { p.cancelText = text; return this; } public ButtomDialog create() { final ButtomDialog dialog = new ButtomDialog(p.context, shadow ? R.style.Theme_Light_NoTitle_Dialog : R.style.Theme_Light_NoTitle_NoShadow_Dialog); Window window = dialog.getWindow(); window.setWindowAnimations(R.style.Animation_Bottom_Rising); window.getDecorView().setPadding(0, 0, 0, 0); WindowManager.LayoutParams lp = window.getAttributes(); lp.width = WindowManager.LayoutParams.MATCH_PARENT; lp.height = WindowManager.LayoutParams.WRAP_CONTENT; window.setAttributes(lp); window.setGravity(Gravity.BOTTOM); View view = LayoutInflater.from(p.context).inflate(R.layout.dialog_bottom_menu, null); TextView btnCancel = (TextView) view.findViewById(R.id.btn_cancel); ViewGroup layContainer = (ViewGroup) view.findViewById(R.id.lay_container); ViewGroup.LayoutParams lpItem = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); ViewGroup.MarginLayoutParams lpDivider = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1); lpDivider.setMargins(50,0,50,0); int dip1 = (int) (1 * p.context.getResources().getDisplayMetrics().density + 0.5f); int spacing = dip1 * 12; boolean hasTitle = !TextUtils.isEmpty(p.menuTitle); if (hasTitle) { //标题样式 TextView tTitle = new TextView(p.context); tTitle.setLayoutParams(lpItem); tTitle.setGravity(Gravity.CENTER); tTitle.setTextColor(p.context.getResources().getColor(R.color.colorAccent)); tTitle.setText(p.menuTitle); tTitle.setPadding(0, spacing, 0, spacing); //单独给标题设置背景样式 // tTitle.setBackgroundResource(R.drawable.common_dialog_selection_selector_top); layContainer.addView(tTitle); View viewDivider = new View(p.context); viewDivider.setLayoutParams(lpDivider); viewDivider.setBackgroundColor(0xFFCED2D6); layContainer.addView(viewDivider); } //每一条的样式 for (int i = 0; i < p.menuList.size(); i++) { BottomMenu bottomMenu = p.menuList.get(i); TextView bbm = new TextView(p.context); bbm.setLayoutParams(lpItem); bbm.setPadding(0, spacing, 0, spacing); bbm.setGravity(Gravity.CENTER); bbm.setText(bottomMenu.funName); bbm.setTextColor(0xFF007AFF); bbm.setTextSize(16); bbm.setOnClickListener(bottomMenu.listener); layContainer.addView(bbm); if (i != p.menuList.size() - 1) { View viewDivider = new View(p.context); viewDivider.setLayoutParams(lpDivider); viewDivider.setBackgroundColor(0xFFCED2D6); layContainer.addView(viewDivider); } } if (!TextUtils.isEmpty(p.cancelText)) { btnCancel.setText(p.cancelText); } if (p.cancelListener != null) { btnCancel.setOnClickListener(p.cancelListener); } else { btnCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); } dialog.setContentView(view); dialog.setCanceledOnTouchOutside(canCancel); dialog.setCancelable(canCancel); return dialog; } } private static class BottomMenu { public String funName; public View.OnClickListener listener; public BottomMenu(String funName, View.OnClickListener listener) { this.funName = funName; this.listener = listener; } } }
四、使用
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button mDialogCustom; private ButtomMenuDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { mDialogCustom = (Button) findViewById(R.id.custom_dialog); mDialogCustom.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.custom_dialog: ButtomMenuDialog.Builder builder = new ButtomMenuDialog.Builder(this); //添加条目,可多个 builder.addMenu("相机", new View.OnClickListener() { @Override public void onClick(View view) { dialog.cancel(); Toast.makeText(MainActivity.this, "相机", Toast.LENGTH_SHORT).show(); } }).addMenu("相册", new View.OnClickListener() { @Override public void onClick(View view) { dialog.cancel(); Toast.makeText(MainActivity.this, "相册", Toast.LENGTH_SHORT).show(); } }); //下面这些设置都可不写 builder.setTitle("这是标题");//添加标题 builder.setCanCancel(false);//点击阴影时是否取消dialog,true为取消 builder.setShadow(true);//是否设置阴影背景,true为有阴影 builder.setCancelText("取消");//设置最下面取消的文本内容 //设置点击取消时的事件 builder.setCancelListener(new View.OnClickListener() { @Override public void onClick(View view) { dialog.cancel(); Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show(); } }); dialog = builder.create(); dialog.show(); break; default: break; } } }
到此,关于“Android中如何使用ButtomDialog自定义底部弹出框”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流