十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)Android中怎么自定義AlertDialog樣式,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
成都創(chuàng)新互聯(lián)公司專注于海陵企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,電子商務(wù)商城網(wǎng)站建設(shè)。海陵網(wǎng)站建設(shè)公司,為海陵等地區(qū)提供建站服務(wù)。全流程按需搭建網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
方法一:完全自定義AlertDialog的layout.如我們要實(shí)現(xiàn)有輸入框的AlertDialog布局custom_dialog.xml:
原來在代碼中使用:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); View view = View .inflate(getActivity(), R.layout.custom_dialog, null); builder.setView(view); builder.setCancelable(true); TextView title= (TextView) view .findViewById(R.id.title);//設(shè)置標(biāo)題 EditText input_edt= (EditText) view .findViewById(R.id.dialog_edit);//輸入內(nèi)容 Button btn_cancel=(Button)view .findViewById(R.id.btn_cancel);//取消按鈕 Button btn_comfirm=(Button)view .findViewById(R.id.btn_comfirm);//確定按鈕 //取消或確定按鈕監(jiān)聽事件處理 AlertDialog dialog = builder.create(); dialog.show();
這樣,我們就可以彈出一個(gè)我們自定義的Dialog。這種方式有個(gè)弊端就是:
如果項(xiàng)目中有多個(gè)UI不同的AlertDialog,我們要寫多個(gè)布局頁面,當(dāng)然可以提取通用布局,然后各種處理。
方法2:通過修改 Android 系統(tǒng)原生的 AlertDialog 中的控件來達(dá)到我們想要的效果。
比如我們要實(shí)現(xiàn)特定風(fēng)格的對話框,我們可以寫個(gè)公共的方法,通過修改 Android 系統(tǒng)原生的 AlertDialog 中的控件來達(dá)到我們想要的效果,簡單代碼如下:
public static void setCustomDialogStyle(AlertDialog dialog){ final Resources res = dialog.getContext().getResources(); int topPanelId = res.getIdentifier("topPanel", "id", "android");//獲取頂部 LinearLayout topPanel = (LinearLayout) getDialog().findViewById(topPanelId); topPanel.setBackgroundResource(R.drawable.dialog_top_bg);//設(shè)置頂部背景 LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, //設(shè)置頂部高度 dp2px(getDialog().getContext(), 50)); topPanel.setLayoutParams(params); int dividerId = res.getIdentifier("titleDivider", "id", "android");//設(shè)置分隔線 View divider = getDialog().findViewById(dividerId); divider.setVisibility(View.GONE); int titleId = res.getIdentifier("alertTitle", "id", "android");//獲取標(biāo)題title TextView title = (TextView) getDialog().findViewById(titleId);//設(shè)置標(biāo)題 title.setTextColor(Color.WHITE);//標(biāo)題文字顏色 title.setTextSize(18);//文字大小 title.setGravity(Gravity.CENTER);//文字位置 int customPanelId = res.getIdentifier("customPanel", "id", "android");//設(shè)置內(nèi)容 FrameLayout customPanel = (FrameLayout) getDialog().findViewById(customPanelId); customPanel.setBackgroundColor(Color.TRANSPARENT);//背景透明 customPanel.getChildAt(0).setBackgroundColor(Color.WHITE); customPanel.setPadding(dp2px(getContext(), 8), 0, ViewUtils.dp2px(getContext(), 8), 0);//設(shè)置padding int buttonPanelId = res.getIdentifier("buttonPanel", "id", "android");//獲取底部 LinearLayout buttonPanel = (LinearLayout) getDialog().findViewById(buttonPanelId); buttonPanel.setBackgroundResource(R.drawable.dialog_bottom_bg);//設(shè)置底部背景 buttonPanel.setPadding(dp2px(getContext(), 8), 1, dp2px(getContext(), 8), 0); Button button1 = (Button) getDialog().findViewById(android.R.id.button1);//設(shè)置底部Button button1.setTextColor(Color.WHITE);//文字顏色 button1.setTextSize(18);//文字大小 button1.setBackgroundResource(R.drawable.bg_right_round);//Button圓形背景框 Button button2 = (Button) getDialog().findViewById(android.R.id.button2); button2.setTextColor(Color.WHITE); button2.setTextSize(18); button2.setBackgroundResource(R.drawable.bg_left_round); }
代碼中用到的各種顏色,背景圖片等根據(jù)需求自己定義。用到的dp與px轉(zhuǎn)換代碼如下:
public static int dp2px(Context context, float dp) { float density = context.getResources().getDisplayMetrics().density; return (int) (dp * density + 0.5f); }
這樣我們就統(tǒng)一定義好了AlertDialog的整個(gè)界風(fēng)格,在使用的時(shí)候,只需要根據(jù)UI需求定義內(nèi)容部分的UI即可。
還是上面可以輸入的AlertDialog,我們的布局就可以只寫成下面這個(gè),當(dāng)然,外面層的LinearLayout也是可以去掉的。
然后在代碼中使用:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("設(shè)置標(biāo)題"); View view = View .inflate(getActivity(), R.layout.custom_dialog, null); builder.setView(view); builder.setCancelable(true); EditText input_edt= (QRCodeEditText) view .findViewById(R.id.input_edt); builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //點(diǎn)擊確定按鈕處理 dialog.cancel(); } } }); builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //點(diǎn)擊取消按鈕處理 dialog.cancel(); } }); final AlertDialog dialog = builder.create(); dialog.show(); setCustomDialogStyle(dialog);//這里不要忘記調(diào)用setCustomDialogStyle方法
這種方式 就比第一種方式方便 多了。當(dāng)然要實(shí)現(xiàn)AlertDialog的背景透明等效果,我們還可以在res/value/style.xml內(nèi)增加以下代碼:
在需要加入alertDialog的地方加入以下語句:
AlertDialog.Builder alertbBuilder=new AlertDialog.Builder(getActivity(),R.style.dialog); //接下來代碼.....
上述就是小編為大家分享的Android中怎么自定義AlertDialog樣式了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。