Android編程自定義AlertDialog樣式的方法詳解
本文實(shí)例講述了Android編程自定義AlertDialog樣式的方法。分享給大家供大家參考,具體如下:
開發(fā)的時(shí)候,通常我們要自定義AlertDialog來滿足我們的功能需求:
比如彈出對(duì)話框中可以輸入信息,或者要展示且有選擇功能的列表,或者要實(shí)現(xiàn)特定的UI風(fēng)格等。那么我們可以通過以下方式來實(shí)現(xiàn)。
方法一:完全自定義AlertDialog的layout.如我們要實(shí)現(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標(biāo)題"
android:textSize="18sp" />
<EditText
android:id="@+id/dialog_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請(qǐng)輸入內(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);//設(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)格的對(duì)話框,我們可以寫個(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也是可以去掉的。
<?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="請(qǐng)輸入內(nèi)容"
android:maxLength="16"
android:textColor="#333333"
android:textSize="16sp" />
</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)增加以下代碼:
<style name="dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item> //Dialog的windowFrame框?yàn)闊o
<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> //設(shè)置dialog的背景
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:backgroundDimAmount">0.7</item> //就是用來控制灰度的值,當(dāng)為1時(shí),界面除了我們的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ā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android自定義Dialog的2種常見方法
- Android自定義Dialog框樣式
- Android自定義Dialog原理實(shí)例解析
- Android 自定義加載動(dòng)畫Dialog彈窗效果的示例代碼
- Android自定義底部彈出框ButtomDialog
- android自定義Dialog彈框和背景陰影顯示效果
- Android自定義Dialog實(shí)現(xiàn)通用圓角對(duì)話框
- Android自定義dialog 自下往上彈出的實(shí)例代碼
- Android 自定義Dialog去除title導(dǎo)航欄的解決方法
- Android自定義Dialog實(shí)現(xiàn)加載對(duì)話框效果
- 解決Android中自定義DialogFragment解決寬度和高度問題
- Android?自定義?Dialog?實(shí)現(xiàn)列表?單選、多選、搜索功能
相關(guān)文章
Android開發(fā)實(shí)現(xiàn)ListView和adapter配合顯示圖片和文字列表功能示例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)ListView和adapter配合顯示圖片和文字列表功能,涉及Android使用ListView結(jié)合adapter適配器實(shí)現(xiàn)圖文顯示功能相關(guān)的布局、解析、權(quán)限控制等操作技巧,需要的朋友可以參考下2019-04-04
android控件實(shí)現(xiàn)單擊拖動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了android控件實(shí)現(xiàn)單擊拖動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01
Android中自定義View的實(shí)現(xiàn)方式總結(jié)大全
這篇文章主要總結(jié)了Android中自定義View的實(shí)現(xiàn)方式的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們學(xué)習(xí)或者使用自定義View具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-04-04
詳解Android中Intent傳遞對(duì)象給Activity的方法
這篇文章主要介紹了Android中Intent傳遞對(duì)象給Activity的方法,文章中對(duì)Activity的生命周期等知識(shí)先作了簡要的介紹,需要的朋友可以參考下2016-04-04
Android實(shí)現(xiàn)點(diǎn)擊切換視圖并跳轉(zhuǎn)傳值
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)點(diǎn)擊切換視圖并跳轉(zhuǎn)傳值,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
實(shí)例講解Android多線程應(yīng)用開發(fā)中Handler的使用
這篇文章主要介紹了Android多線程應(yīng)用開發(fā)中Handler的使用,Handle主要被用來更新UI和處理消息,需要的朋友可以參考下2016-01-01
ToolBar中menu無法同時(shí)顯示圖標(biāo)和文字問題的解決方法
這篇文章主要為大家詳細(xì)介紹了ToolBar中menu無法同時(shí)顯示圖標(biāo)和文字問題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
Android實(shí)現(xiàn)EditText內(nèi)容保存為Bitmap的方法
這篇文章主要介紹了Android實(shí)現(xiàn)EditText內(nèi)容保存為Bitmap的方法,涉及Android中saveBitmap方法的簡單使用技巧,需要的朋友可以參考下2016-01-01

