国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android編程自定義AlertDialog樣式的方法詳解

2019-10-22 18:15:25
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了Android編程自定義AlertDialog樣式的方法。分享給大家供大家參考,具體如下:

開(kāi)發(fā)的時(shí)候,通常我們要自定義AlertDialog來(lái)滿(mǎn)足我們的功能需求:

比如彈出對(duì)話(huà)框中可以輸入信息,或者要展示且有選擇功能的列表,或者要實(shí)現(xiàn)特定的UI風(fēng)格等。那么我們可以通過(guò)以下方式來(lái)實(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>

原來(lái)在代碼中使用:

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    .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)聽(tīng)事件處理AlertDialog dialog = builder.create();dialog.show();

這樣,我們就可以彈出一個(gè)我們自定義的Dialog。這種方式有個(gè)弊端就是:

如果項(xiàng)目中有多個(gè)UI不同的AlertDialog,我們要寫(xiě)多個(gè)布局頁(yè)面,當(dāng)然可以提取通用布局,然后各種處理。

方法2:通過(guò)修改 Android 系統(tǒng)原生的 AlertDialog 中的控件來(lái)達(dá)到我們想要的效果。

比如我們要實(shí)現(xiàn)特定風(fēng)格的對(duì)話(huà)框,我們可以寫(xiě)個(gè)公共的方法,通過(guò)修改 Android 系統(tǒng)原生的 AlertDialog 中的控件來(lái)達(dá)到我們想要的效果,簡(jiǎn)單代碼如下:

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è)置分隔線(xiàn)    View divider = getDialog().findViewById(dividerId);    divider.setVisibility(View.GONE);    int titleId = res.getIdentifier("alertTitle", "id", "android");//獲取標(biāo)題title    TextView    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,我們的布局就可以只寫(xiě)成下面這個(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(wú)     <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> //就是用來(lái)控制灰度的值,當(dāng)為1時(shí),界面除了我們的dialog內(nèi)容是高亮顯示的,dialog以外的區(qū)域是黑色的,完全看不到其他內(nèi)容     <item name="android:backgroundDimEnabled">true</item></style>

在需要加入alertDialog的地方加入以下語(yǔ)句:

AlertDialog.Builder alertbBuilder=new AlertDialog.Builder(getActivity(),R.style.dialog);//接下來(lái)代碼.....

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 浦北县| 江源县| 辉县市| 馆陶县| 巨鹿县| 赫章县| 天长市| 仁布县| 马龙县| 蒲城县| 郎溪县| 荆州市| 古田县| 喀什市| 衡阳县| 沭阳县| 合川市| 奉贤区| 湄潭县| 延安市| 广安市| 博湖县| 霍城县| 延庆县| 阳信县| 铜鼓县| 沙雅县| 丰顺县| 万州区| 吴忠市| 永州市| 乳山市| 江阴市| 石屏县| 嘉禾县| 萨迦县| 慈溪市| 翁牛特旗| 扶风县| 什邡市| 余干县|