<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    The NoteBook of EricKong

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks

    做項目時,感覺android自帶的彈出框樣式比較丑,很多應用都是自己做的彈出框,這里也試著自己做了一個。

    廢話不說先上圖片:



    實現機制

    1.先自定義一個彈出框的樣式

    2.自己實現CustomDialog類,繼承自Dialog,實現里面方法,在里面加載自定義樣式的彈出框;

    3.使用時,與使用Dialog一樣

    具體代碼

    dialog_normal_layout.xml樣式文件

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent"  
    5.     android:clickable="true"  
    6.     android:orientation="vertical"  
    7.     android:padding="20.0dip" >  
    8.   
    9.     <LinearLayout  
    10.         android:layout_width="fill_parent"  
    11.         android:layout_height="wrap_content"  
    12.         android:layout_gravity="center"  
    13.         android:background="@drawable/bg_bombbox"  
    14.         android:orientation="vertical" >  
    15.   
    16.         <TextView  
    17.             android:id="@+id/title"  
    18.             style="@style/text_18_ffffff"  
    19.             android:layout_width="fill_parent"  
    20.             android:layout_height="40.0dip"  
    21.             android:gravity="center"  
    22.             android:text="@string/title_alert"  
    23.             android:visibility="visible" />  
    24.   
    25.         <TextView  
    26.             android:id="@+id/message"  
    27.             style="@style/text_16_666666"  
    28.             android:layout_width="fill_parent"  
    29.             android:layout_height="wrap_content"  
    30.             android:gravity="left|center"  
    31.             android:lineSpacingMultiplier="1.5"  
    32.             android:minHeight="120.0dip"  
    33.             android:paddingBottom="15.0dip"  
    34.             android:paddingLeft="20.0dip"  
    35.             android:paddingRight="20.0dip"  
    36.             android:paddingTop="15.0dip" />  
    37.   
    38.         <View  
    39.             android:layout_width="fill_parent"  
    40.             android:layout_height="1.0px"  
    41.             android:background="#ffd0d0d0" />  
    42.   
    43.         <LinearLayout  
    44.             android:layout_width="fill_parent"  
    45.             android:layout_height="60.0dip"  
    46.             android:layout_gravity="bottom"  
    47.             android:background="@drawable/dialog_bottom_bg"  
    48.             android:gravity="center"  
    49.             android:orientation="horizontal" >  
    50.   
    51.             <Button  
    52.                 android:id="@+id/positiveButton"  
    53.                 style="@style/text_15_ffffff_sdw"  
    54.                 android:layout_width="114.0dip"  
    55.                 android:layout_height="40.0dip"  
    56.                 android:background="@drawable/btn_ok_selector"  
    57.                 android:gravity="center"  
    58.                 android:text="@string/ok" />  
    59.   
    60.             <Button  
    61.                 android:id="@+id/negativeButton"  
    62.                 style="@style/text_15_666666_sdw"  
    63.                 android:layout_width="114.0dip"  
    64.                 android:layout_height="40.0dip"  
    65.                 android:layout_marginLeft="20.0dip"  
    66.                 android:background="@drawable/btn_cancel_selector"  
    67.                 android:gravity="center"  
    68.                 android:text="@string/cancel" />  
    69.         </LinearLayout>  
    70.     </LinearLayout>  
    71.   
    72. </FrameLayout>  

    其中引用的樣式文件styles.xml

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <resources xmlns:android="http://schemas.android.com/apk/res/android">  
    3.   
    4.     <style name="AppBaseTheme" parent="android:Theme.Light"></style>  
    5.   
    6.     <style name="AppTheme" parent="AppBaseTheme"></style>  
    7.   
    8.     <style name="text_18_ffffff">  
    9.         <item name="android:textSize">18.0dip</item>  
    10.         <item name="android:textColor">#ffffffff</item>  
    11.     </style>  
    12.   
    13.     <style name="text_16_666666">  
    14.         <item name="android:textSize">16.0dip</item>  
    15.         <item name="android:textColor">#ff666666</item>  
    16.     </style>  
    17.   
    18.     <style name="sdw_white">  
    19.         <item name="android:shadowColor">#7fffffff</item>  
    20.         <item name="android:shadowDx">0.0</item>  
    21.         <item name="android:shadowDy">0.65</item>  
    22.         <item name="android:shadowRadius">1.0</item>  
    23.     </style>  
    24.   
    25.     <style name="sdw_79351b">  
    26.         <item name="android:shadowColor">#ff79351b</item>  
    27.         <item name="android:shadowDx">0.0</item>  
    28.         <item name="android:shadowDy">1.0</item>  
    29.         <item name="android:shadowRadius">1.0</item>  
    30.     </style>  
    31.   
    32.     <style name="text_15_ffffff_sdw" parent="@style/sdw_79351b">  
    33.         <item name="android:textSize">15.0dip</item>  
    34.         <item name="android:textColor">#ffffffff</item>  
    35.     </style>  
    36.   
    37.     <style name="text_15_666666_sdw" parent="@style/sdw_white">  
    38.         <item name="android:textSize">15.0dip</item>  
    39.         <item name="android:textColor">#ff666666</item>  
    40.     </style>  
    41.   
    42.     <style name="Dialog" parent="android:style/Theme.Dialog">  
    43.         <item name="android:background">#00000000</item>  
    44.         <item name="android:windowBackground">@android:color/transparent</item>  
    45.         <item name="android:windowNoTitle">true</item>  
    46.         <item name="android:windowIsFloating">true</item>  
    47.     </style>  
    48.   
    49. </resources>  

    自定義Dialog的實現類CustomDialog

    1. package com.dyr.custom;  
    2.   
    3. import android.app.Dialog;  
    4. import android.content.Context;  
    5. import android.content.DialogInterface;  
    6. import android.view.LayoutInflater;  
    7. import android.view.View;  
    8. import android.view.ViewGroup.LayoutParams;  
    9. import android.widget.Button;  
    10. import android.widget.LinearLayout;  
    11. import android.widget.TextView;  
    12.   
    13. import com.dyr.view.R;  
    14.   
    15. public class CustomDialog extends Dialog {  
    16.   
    17.     public CustomDialog(Context context) {  
    18.         super(context);  
    19.     }  
    20.   
    21.     public CustomDialog(Context context, int theme) {  
    22.         super(context, theme);  
    23.     }  
    24.   
    25.     public static class Builder {  
    26.         private Context context;  
    27.         private String title;  
    28.         private String message;  
    29.         private String positiveButtonText;  
    30.         private String negativeButtonText;  
    31.         private View contentView;  
    32.         private DialogInterface.OnClickListener positiveButtonClickListener;  
    33.         private DialogInterface.OnClickListener negativeButtonClickListener;  
    34.   
    35.         public Builder(Context context) {  
    36.             this.context = context;  
    37.         }  
    38.   
    39.         public Builder setMessage(String message) {  
    40.             this.message = message;  
    41.             return this;  
    42.         }  
    43.   
    44.         /** 
    45.          * Set the Dialog message from resource 
    46.          *  
    47.          * @param title 
    48.          * @return 
    49.          */  
    50.         public Builder setMessage(int message) {  
    51.             this.message = (String) context.getText(message);  
    52.             return this;  
    53.         }  
    54.   
    55.         /** 
    56.          * Set the Dialog title from resource 
    57.          *  
    58.          * @param title 
    59.          * @return 
    60.          */  
    61.         public Builder setTitle(int title) {  
    62.             this.title = (String) context.getText(title);  
    63.             return this;  
    64.         }  
    65.   
    66.         /** 
    67.          * Set the Dialog title from String 
    68.          *  
    69.          * @param title 
    70.          * @return 
    71.          */  
    72.   
    73.         public Builder setTitle(String title) {  
    74.             this.title = title;  
    75.             return this;  
    76.         }  
    77.   
    78.         public Builder setContentView(View v) {  
    79.             this.contentView = v;  
    80.             return this;  
    81.         }  
    82.   
    83.         /** 
    84.          * Set the positive button resource and it's listener 
    85.          *  
    86.          * @param positiveButtonText 
    87.          * @return 
    88.          */  
    89.         public Builder setPositiveButton(int positiveButtonText,  
    90.                 DialogInterface.OnClickListener listener) {  
    91.             this.positiveButtonText = (String) context  
    92.                     .getText(positiveButtonText);  
    93.             this.positiveButtonClickListener = listener;  
    94.             return this;  
    95.         }  
    96.   
    97.         public Builder setPositiveButton(String positiveButtonText,  
    98.                 DialogInterface.OnClickListener listener) {  
    99.             this.positiveButtonText = positiveButtonText;  
    100.             this.positiveButtonClickListener = listener;  
    101.             return this;  
    102.         }  
    103.   
    104.         public Builder setNegativeButton(int negativeButtonText,  
    105.                 DialogInterface.OnClickListener listener) {  
    106.             this.negativeButtonText = (String) context  
    107.                     .getText(negativeButtonText);  
    108.             this.negativeButtonClickListener = listener;  
    109.             return this;  
    110.         }  
    111.   
    112.         public Builder setNegativeButton(String negativeButtonText,  
    113.                 DialogInterface.OnClickListener listener) {  
    114.             this.negativeButtonText = negativeButtonText;  
    115.             this.negativeButtonClickListener = listener;  
    116.             return this;  
    117.         }  
    118.   
    119.         public CustomDialog create() {  
    120.             LayoutInflater inflater = (LayoutInflater) context  
    121.                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
    122.             // instantiate the dialog with the custom Theme  
    123.             final CustomDialog dialog = new CustomDialog(context,R.style.Dialog);  
    124.             View layout = inflater.inflate(R.layout.dialog_normal_layout, null);  
    125.             dialog.addContentView(layout, new LayoutParams(  
    126.                     LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));  
    127.             // set the dialog title  
    128.             ((TextView) layout.findViewById(R.id.title)).setText(title);  
    129.             // set the confirm button  
    130.             if (positiveButtonText != null) {  
    131.                 ((Button) layout.findViewById(R.id.positiveButton))  
    132.                         .setText(positiveButtonText);  
    133.                 if (positiveButtonClickListener != null) {  
    134.                     ((Button) layout.findViewById(R.id.positiveButton))  
    135.                             .setOnClickListener(new View.OnClickListener() {  
    136.                                 public void onClick(View v) {  
    137.                                     positiveButtonClickListener.onClick(dialog,  
    138.                                             DialogInterface.BUTTON_POSITIVE);  
    139.                                 }  
    140.                             });  
    141.                 }  
    142.             } else {  
    143.                 // if no confirm button just set the visibility to GONE  
    144.                 layout.findViewById(R.id.positiveButton).setVisibility(  
    145.                         View.GONE);  
    146.             }  
    147.             // set the cancel button  
    148.             if (negativeButtonText != null) {  
    149.                 ((Button) layout.findViewById(R.id.negativeButton))  
    150.                         .setText(negativeButtonText);  
    151.                 if (negativeButtonClickListener != null) {  
    152.                     ((Button) layout.findViewById(R.id.negativeButton))  
    153.                             .setOnClickListener(new View.OnClickListener() {  
    154.                                 public void onClick(View v) {  
    155.                                     negativeButtonClickListener.onClick(dialog,  
    156.                                             DialogInterface.BUTTON_NEGATIVE);  
    157.                                 }  
    158.                             });  
    159.                 }  
    160.             } else {  
    161.                 // if no confirm button just set the visibility to GONE  
    162.                 layout.findViewById(R.id.negativeButton).setVisibility(  
    163.                         View.GONE);  
    164.             }  
    165.             // set the content message  
    166.             if (message != null) {  
    167.                 ((TextView) layout.findViewById(R.id.message)).setText(message);  
    168.             } else if (contentView != null) {  
    169.                 // if no message set  
    170.                 // add the contentView to the dialog body  
    171.                 ((LinearLayout) layout.findViewById(R.id.message))  
    172.                         .removeAllViews();  
    173.                 ((LinearLayout) layout.findViewById(R.id.message)).addView(  
    174.                         contentView, new LayoutParams(  
    175.                                 LayoutParams.WRAP_CONTENT,  
    176.                                 LayoutParams.WRAP_CONTENT));  
    177.             }  
    178.             dialog.setContentView(layout);  
    179.             return dialog;  
    180.         }  
    181.   
    182.     }  
    183. }  

    使用代碼

    1. CustomDialog.Builder builder = new CustomDialog.Builder(this);  
    2.         builder.setMessage("這個就是自定義的提示框");  
    3.         builder.setTitle("提示");  
    4.         builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {  
    5.             public void onClick(DialogInterface dialog, int which) {  
    6.                 dialog.dismiss();  
    7.                 //設置你的操作事項  
    8.             }  
    9.         });  
    10.   
    11.         builder.setNegativeButton("取消",  
    12.                 new android.content.DialogInterface.OnClickListener() {  
    13.                     public void onClick(DialogInterface dialog, int which) {  
    14.                         dialog.dismiss();  
    15.                     }  
    16.                 });  
    17.   
    18.         builder.create().show();  

    至此,自定義彈出框已經完成,是不是感覺很簡單呢。

    這里附上一個自定義彈出框的小項目代碼下載地址:點擊打開鏈接

    posted on 2014-12-16 17:24 Eric_jiang 閱讀(426) 評論(0)  編輯  收藏 所屬分類: Android
    主站蜘蛛池模板: 亚洲爆乳无码专区www| 国产高清免费观看| 九九免费久久这里有精品23| 精品亚洲麻豆1区2区3区| 免费a级毛片无码a∨性按摩| 国产精品久久免费| 嫩草在线视频www免费看| 羞羞漫画小舞被黄漫免费| 亚洲一区二区三区乱码在线欧洲| 亚洲Av永久无码精品三区在线| 亚洲欧洲一区二区三区| 日韩高清在线免费观看| 免费AA片少妇人AA片直播| 免费看又黄又无码的网站| 91免费精品国自产拍在线不卡| 一级女性全黄生活片免费看| 亚洲另类无码专区首页| 亚洲综合久久一本伊伊区| 久久精品国产亚洲av影院| 亚洲高清在线视频| 国产V亚洲V天堂无码久久久| 亚洲综合无码AV一区二区| 亚洲av午夜精品一区二区三区 | 亚洲一级片免费看| 国产男女猛烈无遮挡免费视频网站| 男女免费观看在线爽爽爽视频| 日韩免费无码一区二区三区| 国产猛男猛女超爽免费视频| 久久久久久久久久免免费精品| 日日摸夜夜添夜夜免费视频 | 午夜一级免费视频| 欧美三级在线电影免费| 黄+色+性+人免费| 免费黄色网址网站| 国产免费看JIZZ视频| 日韩一区二区a片免费观看 | 亚洲日本乱码一区二区在线二产线| 亚洲精品自在线拍| 亚洲人成免费电影| 亚洲色欲色欱wwW在线| 精品一区二区三区免费毛片|