In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to use ButtomDialog custom bottom pop-up box in Android". In daily operation, I believe many people have doubts about how to use ButtomDialog custom bottom pop-up box in Android. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the question of "how to use ButtomDialog custom bottom pop-up box in Android". Next, please follow the editor to study!
First, configure the resources needed by the custom control.
1. Create an anim folder under the res folder and create two slide_in_bottom.xml and slide_out_bottom.xml files, which are responsible for animation in and out of the pop-up box.
two。 Add shadows and animation styles to style.xml.
@ android:color/transparent true true @ null false @ anim/slide_in_bottom @ anim/slide_out_bottom
3. Create a title_background.xml file under the drawable folder that is responsible for adding background to the text content.
Second, customize the layout of controls
III. Custom control class
Public class ButtomDialog extends Dialog {public ButtomDialog (Context context, int themeResId) {super (context, themeResId);} public static class Params {private final List menuList = 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). Evaluate (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 p.context.getResources (). GetDisplayMetrics (). Density + 0.5f); int spacing = dip1 * 12; boolean hasTitle =! TextUtils.isEmpty (p.menuTitle); if (hasTitle) {/ / title style 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); / / set the background style for the title separately / / 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);} / / the style for of each item (int I = 0; I < p.menuList.size (); iTunes +) {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;}
IV. Use
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); / / add multiple builder.addMenu ("camera", new View.OnClickListener () {@ Override public void onClick (View view) {dialog.cancel ()) Toast.makeText (MainActivity.this, "camera", Toast.LENGTH_SHORT). Show ();}}) .addMenu ("album", new View.OnClickListener () {@ Override public void onClick (View view) {dialog.cancel (); Toast.makeText (MainActivity.this, "album", Toast.LENGTH_SHORT) .show () }}); / / the following settings can not write builder.setTitle ("this is the title"); / / add the title builder.setCanCancel (false); / / whether to cancel the dialog,true when clicking the shadow is to cancel the builder.setShadow (true); / / whether to set the shadow background, true is the shadow builder.setCancelText ("cancel") / / set the text content cancelled at the bottom / / set the event builder.setCancelListener when you click cancel (new View.OnClickListener () {@ Override public void onClick (View view) {dialog.cancel (); Toast.makeText (MainActivity.this, "cancel", Toast.LENGTH_SHORT). Show ();}}) Dialog = builder.create (); dialog.show (); break; default: break;} this is the end of the study on "how to use ButtomDialog to customize the bottom pop-up box in Android", hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.