十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
Android中怎么自定義view實(shí)現(xiàn)彈出框效果?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
成都創(chuàng)新互聯(lián)公司網(wǎng)站建設(shè)公司是一家服務(wù)多年做網(wǎng)站建設(shè)策劃設(shè)計(jì)制作的公司,為廣大用戶提供了成都做網(wǎng)站、成都網(wǎng)站建設(shè),成都網(wǎng)站設(shè)計(jì),1元廣告,成都做網(wǎng)站選成都創(chuàng)新互聯(lián)公司,貼合企業(yè)需求,高性價(jià)比,滿足客戶不同層次的需求一站式服務(wù)歡迎致電。
1.layout布局文件
view_actionsheet.xml
view_alertdialog.xml
2.style.xml文件
3.color.xml文件
#3F51B5 #303F9F #FF4081 #000000 #00000000 #c6c6c6 #037BFF #FD4A2E #8F8F8F
4.dimen.xml文件
16dp 16dp 20sp
5.anim動(dòng)畫
actionsheet_dialog_in.xml
actionsheet_dialog_out.xml
6.drawable文件夾的諸多資源
資源下載來源
7.底部彈出框
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 ListsheetItemList; private Display display; public ActionSheetDialog(Context context) { this.context = context; WindowManager windowManager = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); display = windowManager.getDefaultDisplay(); } public ActionSheetDialog builder() { // 獲取Dialog布局 View view = LayoutInflater.from(context).inflate( R.layout.view_actionsheet, null); // 設(shè)置Dialog最小寬度為屏幕寬度 view.setMinimumWidth(display.getWidth()); // 獲取自定義Dialog布局中的控件 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(); } }); // 定義Dialog布局和參數(shù) 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 條目名稱 * @param color 條目字體顏色,設(shè)置null則默認(rèn)藍(lán)色 * @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; } /** * 設(shè)置條目布局 */ private void setSheetItems() { if (sheetItemList == null || sheetItemList.size() <= 0) { return; } int size = sheetItemList.size(); // TODO 高度控制,非最佳解決辦法 // 添加條目過多的時(shí)候控制高度 if (size >= 7) { ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) sLayout_content .getLayoutParams(); params.height = display.getHeight() / 2; sLayout_content.setLayoutParams(params); } // 循環(huán)添加條目 for (int i = 1; i <= size; i++) { final int index = i; SheetItem sheetItem = sheetItemList.get(i - 1); String strItem = sheetItem.name; SheetItemColor color = sheetItem.color; final OnSheetItemClickListener listener = (OnSheetItemClickListener) sheetItem.itemClickListener; TextView textView = new TextView(context); textView.setText(strItem); textView.setTextSize(18); textView.setGravity(Gravity.CENTER); // 背景圖片 if (size == 1) { if (showTitle) { textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector); } else { textView.setBackgroundResource(R.drawable.actionsheet_single_selector); } } else { if (showTitle) { if (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); } } } // 字體顏色 if (color == null) { textView.setTextColor(Color.parseColor(SheetItemColor.Blue .getName())); } else { textView.setTextColor(Color.parseColor(color.getName())); } // 高度 float scale = context.getResources().getDisplayMetrics().density; int height = (int) (45 * scale + 0.5f); textView.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, height)); // 點(diǎn)擊事件 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.中間彈出框
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() { // 獲取Dialog布局 View view = LayoutInflater.from(context).inflate( R.layout.view_alertdialog, null); // 獲取自定義Dialog布局中的控件 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); // 定義Dialog布局和參數(shù) dialog = new Dialog(context, R.style.AlertDialogStyle); dialog.setContentView(view); // 調(diào)整dialog背景大小 lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display .getWidth() * 0.85), ViewGroup.LayoutParams.WRAP_CONTENT)); return this; } public AlertDialog setTitle(String title) { showTitle = true; if ("".equals(title)) { txt_title.setText("標(biāo)題"); } else { txt_title.setText(title); } return this; } public AlertDialog setMsg(String msg) { showMsg = true; if ("".equals(msg)) { txt_msg.setText("內(nèi)容"); } 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("確定"); } 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("取消"); } 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("提示"); 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("確定"); 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.具體使用
activity調(diào)用
@Override public void onClick(View v) { switch (v.getId()) { case R.id.click: // TODO 18/03/16 new ActionSheetDialog(this) .builder() .setTitle("清空消息列表后,聊天記錄依然保留,確定要清空消息列表?") .setCancelable(false) .setCanceledOnTouchOutside(false) .addSheetItem("清空消息列表", 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("發(fā)送給好友", ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener() { @Override public void onClick(int which) { } }) .addSheetItem("轉(zhuǎn)載到空間相冊(cè)", ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener() { @Override public void onClick(int which) { } }) .addSheetItem("上傳到群相冊(cè)", ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener() { @Override public void onClick(int which) { } }) .addSheetItem("保存到手機(jī)", ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener() { @Override public void onClick(int which) { } }) .addSheetItem("收藏", ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener() { @Override public void onClick(int which) { } }) .addSheetItem("查看聊天圖片", ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener() { @Override public void onClick(int which) { } }).show(); break; case R.id.ctrol:// TODO 18/03/16 new ActionSheetDialog(this) .builder() .setTitle("請(qǐng)選擇操作") .setCancelable(false) .setCanceledOnTouchOutside(false) .addSheetItem("條目一", ActionSheetDialog.SheetItemColor.Blue, new ActionSheetDialog.OnSheetItemClickListener() { @Override public void onClick(int which) { } }) .addSheetItem("條目二", 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("退出當(dāng)前賬號(hào)") .setMsg("再連續(xù)登陸15天,就可變身為QQ達(dá)人。退出QQ可能會(huì)使你現(xiàn)有記錄歸零,確定退出?") .setPositiveButton("確認(rèn)退出", new View.OnClickListener() { @Override public void onClick(View v) { } }).setNegativeButton("取消", new View.OnClickListener() { @Override public void onClick(View v) { } }).show(); break; case R.id.msg:// TODO 18/03/16 new AlertDialog(this).builder() .setMsg("你現(xiàn)在無法接收到新消息提醒。請(qǐng)到系統(tǒng)-設(shè)置-通知中開啟消息提醒") .setNegativeButton("確定", new View.OnClickListener() { @Override public void onClick(View v) { } }).show(); break; default: break; } }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。