In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how Android customizes the effect of view imitating iOS pop-up boxes. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
1.layout layout file
View_actionsheet.xml
View_alertdialog.xml
2.style.xml file
Android:color/transparent @ null true @ null true true true @ style/ActionSheetDialogAnimation @ anim/actionsheet_dialog_in @ anim/actionsheet_dialog_out @ color/actionsheet_blue @ dimen/nav_title_text_size @ android:color/transparent @ null true @ null true true true
3.color.xml file
# 3F51B5 # 303F9F # FF4081 # 000000 # 00000000 # c6c6c6 # 037BFF # FD4A2E # 8F8F8F
4.dimen.xml file
16dp 16dp 20sp
5.anim animation
Actionsheet_dialog_in.xml
Actionsheet_dialog_out.xml
Many resources of the 6.drawable folder
Source of resource download
7. Bottom pop-up box
Import android.app.Dialog;import android.content.Context;import android.graphics.Color;import android.view.Display;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.view.WindowManager;import android.widget.LinearLayout;import android.widget.ScrollView;import android.widget.TextView;import java.util.ArrayList;import java.util.List / * * Author:AND * Time:2018/3/16. * Email:2911743255@qq.com * Description: * Detail: * / public class ActionSheetDialog {private Context context; private Dialog dialog; private TextView txt_title; private TextView txt_cancel; private LinearLayout lLayout_content; private ScrollView sLayout_content; private boolean showTitle = false; private List sheetItemList; private Display display; public ActionSheetDialog (Context context) {this.context = context; WindowManager windowManager = (WindowManager) context .getSystemService (Context.WINDOW_SERVICE); display = windowManager.getDefaultDisplay () } public ActionSheetDialog builder () {/ / get Dialog layout View view = LayoutInflater.from (context). Inflate (R.layout.view_actionsheet, null); / / set the minimum width of Dialog to screen width view.setMinimumWidth (display.getWidth ()); / / get controls in custom Dialog layout sLayout_content = (ScrollView) view.findViewById (R.id.sLayout_content); lLayout_content = (LinearLayout) view.findViewById (R.id.lLayout_content) Txt_title = (TextView) view.findViewById (R.id.txt_title); txt_cancel = (TextView) view.findViewById (R.id.txt_cancel); txt_cancel.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {dialog.dismiss ();}}); / / define Dialog layout and parameters dialog = new Dialog (context, R.style.ActionSheetDialogStyle); dialog.setContentView (view); Window dialogWindow = dialog.getWindow () DialogWindow.setGravity (Gravity.LEFT | Gravity.BOTTOM); WindowManager.LayoutParams lp = dialogWindow.getAttributes (); lp.x = 0; lp.y = 0; dialogWindow.setAttributes (lp); return this;} public ActionSheetDialog setTitle (String title) {showTitle = true; txt_title.setVisibility (View.VISIBLE); txt_title.setText (title); return this;} public ActionSheetDialog setCancelable (boolean cancel) {dialog.setCancelable (cancel); return this;} public ActionSheetDialog setCanceledOnTouchOutside (boolean cancel) {dialog.setCanceledOnTouchOutside (cancel); return this } / * @ param strItem entry name * @ param color entry font color. Setting null defaults to blue * @ param listener * @ return * / public ActionSheetDialog addSheetItem (String strItem, SheetItemColor color, OnSheetItemClickListener listener) {if (sheetItemList = = null) {sheetItemList = new ArrayList ();} sheetItemList.add (new SheetItem (strItem, color, listener)); return this } / * set entry layout * / private void setSheetItems () {if (sheetItemList = = null | | sheetItemList.size () = 7) {ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) sLayout_content .getLayoutParams (); params.height = display.getHeight () / 2; sLayout_content.setLayoutParams (params);} / / add entry for (int I = 1; I = 1 & & I < size) {textView.setBackgroundResource (R.drawable.actionsheet_middle_selector) } else {textView.setBackgroundResource (R.drawable.actionsheet_bottom_selector);}} else {if (I = = 1) {textView.setBackgroundResource (R.drawable.actionsheet_top_selector);} else if (I < size) {textView.setBackgroundResource (R.drawable.actionsheet_middle_selector);} else {textView.setBackgroundResource (R.drawable.actionsheet_bottom_selector) } / / Font color if (color = = null) {textView.setTextColor (Color.parseColor (SheetItemColor.Blue. GetName ();} else {textView.setTextColor (Color.parseColor (color.getName ();} / / height float scale = context.getResources (). GetDisplayMetrics (). Density; int height = (int) (45 * scale + 0.5f); textView.setLayoutParams (new ViewGroup.LayoutParams (ViewGroup.LayoutParams.MATCH_PARENT, height)) / Click event textView.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {listener.onClick (index); dialog.dismiss ();}}); lLayout_content.addView (textView);}} public void show () {setSheetItems (); dialog.show ();} public interface OnSheetItemClickListener {void onClick (int which);} public class SheetItem {String name; OnSheetItemClickListener itemClickListener; SheetItemColor color; public SheetItem (String name, SheetItemColor color, OnSheetItemClickListener itemClickListener) {this.name = name This.color = color; this.itemClickListener = itemClickListener;}} public enum SheetItemColor {Blue ("# 037BFF"), Red ("# FD4A2E"); private String name; private SheetItemColor (String name) {this.name = name;} public String getName () {return name;} public void setName (String name) {this.name = name;}
8. Middle pop-up box
Import android.app.Dialog;import android.content.Context;import android.view.Display;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.WindowManager;import android.widget.Button;import android.widget.FrameLayout;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;/** * Author:AND * Time:2018/3/16. * Email:2911743255@qq.com * Description: * Detail: * / public class AlertDialog {private Context context; private Dialog dialog Private LinearLayout lLayout_bg; private TextView txt_title; private TextView txt_msg; private Button btn_neg; private Button btn_pos; private ImageView img_line; private Display display; private boolean showTitle = false; private boolean showMsg = false; private boolean showPosBtn = false; private boolean showNegBtn = false; public AlertDialog (Context context) {this.context = context; WindowManager windowManager = (WindowManager) context .getSystemService (Context.WINDOW_SERVICE); display = windowManager.getDefaultDisplay () } public AlertDialog builder () {/ / get Dialog layout View view = LayoutInflater.from (context). Inflate (R.layout.view_alertdialog, null); / / get the controls in the custom Dialog layout lLayout_bg = (LinearLayout) view.findViewById (R.id.lLayout_bg); txt_title = (TextView) view.findViewById (R.id.txt_title); txt_title.setVisibility (View.GONE); txt_msg = (TextView) view.findViewById (R.id.txt_msg) Txt_msg.setVisibility (View.GONE); btn_neg = (Button) view.findViewById (R.id.btn_neg); btn_neg.setVisibility (View.GONE); btn_pos = (Button) view.findViewById (R.id.btn_pos); btn_pos.setVisibility (View.GONE); img_line = (ImageView) view.findViewById (R.id.img_line); img_line.setVisibility (View.GONE); / / define Dialog layout and parameters dialog = new Dialog (context, R.style.AlertDialogStyle) Dialog.setContentView (view); / / Resize dialog background lLayout_bg.setLayoutParams ((int) (display .getWidth () * 0.85), ViewGroup.LayoutParams.WRAP_CONTENT); return this;} public AlertDialog setTitle (String title) {showTitle = true; if (".equals (title)) {txt_title.setText (" title ");} else {txt_title.setText (title);} return this;} public AlertDialog setMsg (String msg) {showMsg = true If (".equals (msg)) {txt_msg.setText (" content ");} else {txt_msg.setText (msg);} return this;} public AlertDialog setCancelable (boolean cancel) {dialog.setCancelable (cancel); return this;} public AlertDialog setPositiveButton (String text, final View.OnClickListener listener) {showPosBtn = true; if (" .equals (text)) {btn_pos.setText ("determine");} else {btn_pos.setText (text) } btn_pos.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {listener.onClick (v); dialog.dismiss ();}}); return this;} public AlertDialog setNegativeButton (String text, final View.OnClickListener listener) {showNegBtn = true; if (".equals (text)) {btn_neg.setText (" cancel ");} else {btn_neg.setText (text) } btn_neg.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {listener.onClick (v); dialog.dismiss ();}}); return this;} private void setLayout () {if (! showTitle & &! showMsg) {txt_title.setText ("prompt"); txt_title.setVisibility (View.VISIBLE);} if (showTitle) {txt_title.setVisibility (View.VISIBLE);} if (showMsg) {txt_msg.setVisibility (View.VISIBLE) } if (! showPosBtn & &! showNegBtn) {btn_pos.setText (OK); btn_pos.setVisibility (View.VISIBLE); btn_pos.setBackgroundResource (R.drawable.alertdialog_single_selector); btn_pos.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {dialog.dismiss ();}});} if (showPosBtn & & showNegBtn) {btn_pos.setVisibility (View.VISIBLE); btn_pos.setBackgroundResource (R.drawable.alertdialog_right_selector) Btn_neg.setVisibility (View.VISIBLE); btn_neg.setBackgroundResource (R.drawable.alertdialog_left_selector); img_line.setVisibility (View.VISIBLE);} if (showPosBtn & &! showNegBtn) {btn_pos.setVisibility (View.VISIBLE); btn_pos.setBackgroundResource (R.drawable.alertdialog_single_selector);} if (! showPosBtn & & showNegBtn) {btn_neg.setVisibility (View.VISIBLE); btn_neg.setBackgroundResource (R.drawable.alertdialog_single_selector) }} public void show () {setLayout (); dialog.show ();}}
9. Specific use
Activity call
@ Override public void onClick (View v) {switch (v.getId ()) {case R.id.click: / / TODO 18-03-16 new ActionSheetDialog (this) .builder () .setTitle ("after emptying the message list, the chat record is still retained, are you sure you want to empty the message list?") .setCancelable (false) .setCanceledOnTouchOutside (false) .addSheetItem ("clear message list", ActionSheetDialog.SheetItemColor.Red, new ActionSheetDialog.OnSheetItemClickListener () {@ Override public void onClick (int which) {}) .show (); break Case R.id.iamge:// TODO 18-03-16 new ActionSheetDialog (this) .builder () .setCancelable (false) .setCanceledOnTouchOutside (false) .addSheetItem ("send to Friends", ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener () {@ Override public void onClick (int which) {}) .addSheetItem ("reprint to Space album", ActionSheetDialog.SheetItemColor.Blue New ActionSheetDialog.OnSheetItemClickListener () {@ Override public void onClick (int which) {}) .addSheetItem ("upload to group album", ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener () {@ Override public void onClick (int which) {}) .addSheetItem ("Save to Mobile", ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener () {@ Override public void onClick (int which) {}) .addSheetItem ("favorites", ActionSheetDialog.SheetItemColor.Blue New ActionSheetDialog.OnSheetItemClickListener () {@ Override public void onClick (int which) {}) .addSheetItem ("View chat pictures", ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener () {@ Override public void onClick (int which) {}) .SheetItem () Break Case R.id.ctrol:// TODO 18-03-16 new ActionSheetDialog (this) .builder () .setTitle ("Please select action") .setCancelable (false) .setCanceledOnTouchOutside (false) .addSheetItem ("item one", ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener () {@ Override public void onClick (int which) {}) .addSheetItem ("item two", ActionSheetDialog.SheetItemColor.Blue) New ActionSheetDialog.OnSheetItemClickListener () {@ Override public void onClick (int which) {}}) .show () Break; case R.id.login:// TODO 18-03-16 new AlertDialog (this). Builder (). SetTitle ("quit the current account") .setMsg ("log in for another 15 days, you can become a QQ talent. Quitting QQ may make your existing records go back to zero, so are you sure to quit?) .setPositiveButton ("confirm exit", new View.OnClickListener () {@ Override public void onClick (View v) {}}) .setNegativeButton ("cancel", new View.OnClickListener () {@ Override public void onClick (View v) {}}) .show (); break; case R.id.msg:// TODO 18-03-16 new AlertDialog (this). Builder () .setMsg ("you cannot receive a new message reminder now. Go to system-Settings-Notification to enable message reminder ") .setNegativeButton (" OK ", new View.OnClickListener () {@ Override public void onClick (View v) {}}) .show (); break; default: break;}}
On "Android how to customize view iOS pop-up box effect" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.
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.