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

首頁 > 系統 > Android > 正文

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

2019-12-12 01:10:38
字體:
來源:轉載
供稿:網友

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

開發的時候,通常我們要自定義AlertDialog來滿足我們的功能需求:

比如彈出對話框中可以輸入信息,或者要展示且有選擇功能的列表,或者要實現特定的UI風格等。那么我們可以通過以下方式來實現。

方法一:完全自定義AlertDialog的layout.如我們要實現有輸入框的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="請輸入內容"    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);//輸入內容Button btn_cancel=(Button)view.findViewById(R.id.btn_cancel);//取消按鈕 Button btn_comfirm=(Button)view.findViewById(R.id.btn_comfirm);//確定按鈕//取消或確定按鈕監聽事件處理AlertDialog dialog = builder.create();dialog.show();

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

如果項目中有多個UI不同的AlertDialog,我們要寫多個布局頁面,當然可以提取通用布局,然后各種處理。

方法2:通過修改 Android 系統原生的 AlertDialog 中的控件來達到我們想要的效果。

比如我們要實現特定風格的對話框,我們可以寫個公共的方法,通過修改 Android 系統原生的 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");//設置內容    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);}

代碼中用到的各種顏色,背景圖片等根據需求自己定義。用到的dp與px轉換代碼如下:

public static int dp2px(Context context, float dp) {    float density = context.getResources().getDisplayMetrics().density;    return (int) (dp * density + 0.5f);}

這樣我們就統一定義好了AlertDialog的整個界風格,在使用的時候,只需要根據UI需求定義內容部分的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="請輸入內容"    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);//這里不要忘記調用setCustomDialogStyle方法

這種方式 就比第一種方式方便 多了。當然要實現AlertDialog的背景透明等效果,我們還可以在res/value/style.xml內增加以下代碼:

<style name="dialog" parent="@android:style/Theme.Dialog">     <item name="android:windowFrame">@null</item> //Dialog的windowFrame框為無     <item name="android:windowIsFloating">true</item> //是否浮現在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內容是高亮顯示的,dialog以外的區域是黑色的,完全看不到其他內容     <item name="android:backgroundDimEnabled">true</item></style>

在需要加入alertDialog的地方加入以下語句:

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

更多關于Android相關內容感興趣的讀者可查看本站專題:《Android開發入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 蒲江县| 海阳市| 无锡市| 虹口区| 通海县| 鹿泉市| 衡东县| 江阴市| 舒城县| 汶上县| 上思县| 九龙县| 佛山市| 巢湖市| 满城县| 扎兰屯市| 五常市| 巴彦县| 旺苍县| 竹溪县| 招远市| 洛隆县| 景德镇市| 江津市| 沧州市| 手机| 齐河县| 宜良县| 阳曲县| 通州市| 泰州市| 霞浦县| 东平县| 伊宁县| 雅安市| 册亨县| 鄂伦春自治旗| 盐山县| 山阴县| 蚌埠市| 休宁县|