Android編程自定義AlertDialog樣式的方法詳解
本文實例講述了Android編程自定義AlertDialog樣式的方法。分享給大家供大家參考,具體如下:
開發(fā)的時候,通常我們要自定義AlertDialog來滿足我們的功能需求:
比如彈出對話框中可以輸入信息,或者要展示且有選擇功能的列表,或者要實現(xiàn)特定的UI風格等。那么我們可以通過以下方式來實現(xiàn)。
方法一:完全自定義AlertDialog的layout.如我們要實現(xiàn)有輸入框的AlertDialog布局custom_dialog.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/dialog_bg" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#00ffff" android:gravity="center" android:padding="10dp" android:text="Dialog標題" android:textSize="18sp" /> <EditText android:id="@+id/dialog_edit" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入內(nèi)容" android:minLines="2" android:textScaleX="16sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal"> <Button android:id="@+id/btn_cancel" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="#00ffff" android:text="取消" /> <View android:layout_width="1dp" android:layout_height="40dp" android:background="#D1D1D1"></View> <Button android:id="@+id/btn_comfirm" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="#00ffff" android:text="確定" /> </LinearLayout> </LinearLayout>
原來在代碼中使用:
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);//設置標題 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();
這樣,我們就可以彈出一個我們自定義的Dialog。這種方式有個弊端就是:
如果項目中有多個UI不同的AlertDialog,我們要寫多個布局頁面,當然可以提取通用布局,然后各種處理。
方法2:通過修改 Android 系統(tǒng)原生的 AlertDialog 中的控件來達到我們想要的效果。
比如我們要實現(xiàn)特定風格的對話框,我們可以寫個公共的方法,通過修改 Android 系統(tǒng)原生的 AlertDialog 中的控件來達到我們想要的效果,簡單代碼如下:
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);//設置頂部背景 LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, //設置頂部高度 dp2px(getDialog().getContext(), 50)); topPanel.setLayoutParams(params); int dividerId = res.getIdentifier("titleDivider", "id", "android");//設置分隔線 View divider = getDialog().findViewById(dividerId); divider.setVisibility(View.GONE); int titleId = res.getIdentifier("alertTitle", "id", "android");//獲取標題title TextView title = (TextView) getDialog().findViewById(titleId);//設置標題 title.setTextColor(Color.WHITE);//標題文字顏色 title.setTextSize(18);//文字大小 title.setGravity(Gravity.CENTER);//文字位置 int customPanelId = res.getIdentifier("customPanel", "id", "android");//設置內(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);//設置padding int buttonPanelId = res.getIdentifier("buttonPanel", "id", "android");//獲取底部 LinearLayout buttonPanel = (LinearLayout) getDialog().findViewById(buttonPanelId); buttonPanel.setBackgroundResource(R.drawable.dialog_bottom_bg);//設置底部背景 buttonPanel.setPadding(dp2px(getContext(), 8), 1, dp2px(getContext(), 8), 0); Button button1 = (Button) getDialog().findViewById(android.R.id.button1);//設置底部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的整個界風格,在使用的時候,只需要根據(jù)UI需求定義內(nèi)容部分的UI即可。
還是上面可以輸入的AlertDialog,我們的布局就可以只寫成下面這個,當然,外面層的LinearLayout也是可以去掉的。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_content" android:layout_height="wrap_content" android:orientation="vertical"> <EditText android:id="@+id/input_edt" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="@drawable/input" android:hint="請輸入內(nèi)容" android:maxLength="16" android:textColor="#333333" android:textSize="16sp" /> </LinearLayout>
然后在代碼中使用:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("設置標題"); 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) { //點擊確定按鈕處理 dialog.cancel(); } } }); builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //點擊取消按鈕處理 dialog.cancel(); } }); final AlertDialog dialog = builder.create(); dialog.show(); setCustomDialogStyle(dialog);//這里不要忘記調(diào)用setCustomDialogStyle方法
這種方式 就比第一種方式方便 多了。當然要實現(xiàn)AlertDialog的背景透明等效果,我們還可以在res/value/style.xml內(nèi)增加以下代碼:
<style name="dialog" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> //Dialog的windowFrame框為無 <item name="android:windowIsFloating">true</item> //是否浮現(xiàn)在activity之上 <item name="android:windowIsTranslucent">true</item> //是否半透明 <item name="android:windowNoTitle">true</item> //是否顯示title <item name="android:background">@android:color/transparent</item> //設置dialog的背景 <item name="android:windowBackground">@android:color/transparent</item> <item name="android:backgroundDimAmount">0.7</item> //就是用來控制灰度的值,當為1時,界面除了我們的dialog內(nèi)容是高亮顯示的,dialog以外的區(qū)域是黑色的,完全看不到其他內(nèi)容 <item name="android:backgroundDimEnabled">true</item> </style>
在需要加入alertDialog的地方加入以下語句:
AlertDialog.Builder alertbBuilder=new AlertDialog.Builder(getActivity(),R.style.dialog); //接下來代碼.....
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設計有所幫助。
- Android自定義Dialog的2種常見方法
- Android自定義Dialog框樣式
- Android自定義Dialog原理實例解析
- Android 自定義加載動畫Dialog彈窗效果的示例代碼
- Android自定義底部彈出框ButtomDialog
- android自定義Dialog彈框和背景陰影顯示效果
- Android自定義Dialog實現(xiàn)通用圓角對話框
- Android自定義dialog 自下往上彈出的實例代碼
- Android 自定義Dialog去除title導航欄的解決方法
- Android自定義Dialog實現(xiàn)加載對話框效果
- 解決Android中自定義DialogFragment解決寬度和高度問題
- Android?自定義?Dialog?實現(xiàn)列表?單選、多選、搜索功能
相關(guān)文章
Android實現(xiàn)自定義圓角對話框Dialog的示例代碼
項目中多處用到對話框,本篇文章主要介紹了Android實現(xiàn)圓角對話框Dialog的示例代碼,有興趣的可以了解一下。2017-03-03Eclipse+ADT+Android SDK搭建安卓開發(fā)環(huán)境的實現(xiàn)步驟
這篇文章主要介紹了Eclipse+ADT+Android SDK搭建安卓開發(fā)環(huán)境的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09Android編程實現(xiàn)手機自帶內(nèi)部存儲路徑的獲取方法
這篇文章主要介紹了Android編程實現(xiàn)手機自帶內(nèi)部存儲路徑的獲取方法,涉及Android針對掛載點信息的獲取技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11