Android 修改dialog背景

Android 修改dialog背景

styles.xml

Java代码  点击浏览下一页
  1. <style name="NewBorderDialogTheme" parent="android:Theme.Dialog">    
  2.  <item name="android:windowBackground">@drawable/your_drawable/item>     
  3. </style>   

 

Java代码  点击浏览下一页
  1. import android.app.AlertDialog;   
  2. import android.content.Context;   
  3.   
  4. import android.content.res.TypedArray;   
  5. import android.content.res.Resources.Theme;   
  6.   
  7. import  android.graphics.PorterDuff;   
  8. import android.graphics.drawable.Drawable;   
  9.   
  10. import android.view.View;   
  11. import android.view.ViewGroup;   
  12. import android.widget.Button;   
  13. import android.widget.ImageView;   
  14.   
  15.   
  16. public class MyColorDialog  extends AlertDialog{   
  17.      private static int NONE = -1;   
  18.      private int tint = NONE;   
  19.      
  20.      /**  
  21.       * @param context  
  22.      * @param theme  
  23.      */  
  24.       protected MyColorDialog(Context context) {   
  25.         super(context);   
  26.         init();   
  27.           
  28.     }   
  29.       
  30.    /**  
  31.     * @param context  
  32.     * @param theme  
  33.      */  
  34.    protected MyColorDialog(Context context, int theme) {   
  35.        super(context, theme);   
  36.       init();   
  37.     }   
  38.        
  39.     /**  
  40.      *   
  41.     */  
  42.     private void init() {         
  43.       <SPAN style="COLOR: #ff0000">final Theme theme = getContext().getTheme();   
  44.        final TypedArray attrs = theme.obtainStyledAttributes(new int[] { android.R.attr.tint });   
  45.       tint = attrs.getColor(0, NONE);</SPAN>   
  46.    }   
  47.   
  48.     @Override  
  49.     public void show() {   
  50.         // TODO Auto-generated method stub   
  51.         super.show();   
  52.          setTint(tint);   
  53.     }   
  54.   
  55.     public void setTint(int tint) {   
  56.         // TODO Auto-generated method stub   
  57.          this.tint = tint;   
  58.          android.graphics.PorterDuff.Mode mode = PorterDuff.Mode.SRC_ATOP;   
  59.             
  60.           
  61.             final Drawable d =  this.getWindow().getDecorView().getBackground();   
  62.              
  63.                d.mutate().setColorFilter(tint, mode);   
  64.              
  65.                 }   
  66.        
  67.   
  68.     /**  
  69.      * @param button  
  70.      */  
  71.     public void setCancelButton(Button button) {   
  72.        button.setOnClickListener(new View.OnClickListener() {   
  73.     
  74.           @Override  
  75.          public void onClick(View v) {   
  76.             cancel();   
  77.           }   
  78.        
  79.        });   
  80.     }   
  81.        
  82.     /**  
  83.      * @param button  
  84.      */  
  85.     public void setPositiveButton(Button button) {   
  86.        button.setOnClickListener(new View.OnClickListener() {   
  87.     
  88.           @Override  
  89.           public void onClick(View v) {   
  90.              dismiss();   
  91.           }   
  92.        
  93.        });   
  94.     }   
  95.     
  96.     
  97.        
  98.     public static class Builder extends AlertDialog.Builder {   
  99.   
  100.        private MyColorDialog dialog;   
  101.          
  102.        public Builder(Context context) {   
  103.           super(context);   
  104.              
  105.           dialog = new MyColorDialog(context);   
  106.        }   
  107.   
  108.        public Builder(Context context, int theme) {   
  109.           super(context);   
  110.           dialog = new MyColorDialog(context, theme);   
  111.        }   
  112.           
  113.          
  114.   
  115.     @Override  
  116.        public MyColorDialog create() {   
  117.           return dialog;   
  118.        }   
  119.     
  120.        @Override  
  121.        public Builder setMessage(CharSequence message) {   
  122.           dialog.setMessage(message);   
  123.           return this;   
  124.        }   
  125.     
  126.        @Override  
  127.        public Builder setTitle(CharSequence title) {   
  128.           dialog.setTitle(title);   
  129.           return this;   
  130.        }   
  131.     
  132.        @Override  
  133.        public Builder setPositiveButton(CharSequence text,   
  134.              OnClickListener listener) {   
  135.           dialog.setButton(BUTTON_POSITIVE, text, listener);   
  136.           return this;   
  137.        }   
  138.     
  139.           @Override  
  140.           public Builder setIcon(int iconId) {   
  141.              dialog.setIcon(iconId);   
  142.              return this;   
  143.           }   
  144.     
  145.     }   
  146.   
  147.   
  148.        
  149.   
  150. }  

 使用

  new MyColorDialog.Builder(this, R.style.OrangeDialogTheme).setPositiveButton("Dismiss", null).setTitle("Warning").setMessage("adhuhfdu").create().show();

 

Java代码  点击浏览下一页
  1. <?xml version="1.0" encoding="utf-8"?>   
  2.  <resources>   
  3.     <color name="red_tint">#88FF0000</color>   
  4.     <color name="blue_tint">#880000FF</color>   
  5.     <color name="yellow_tint">#88FFFF00</color>   
  6.     <color name="purple_tint">#88995f86</color>   
  7.     <color name="orange_tint">#aaffbf00</color>   
  8.     <color name="magenta_tint">#88ff33cc</color>   
  9.     <color name="transparent">#0000</color>   
  10.  </resources>   

 

Java代码  点击浏览下一页
  1. <?xml version="1.0" encoding="utf-8"?>   
  2.  <resources>   
  3.     <!-- Orange -->   
  4.     <style name="OrangeDialogTheme" parent="@android:style/Theme.Dialog">   
  5.       <item name="android:tint">@color/orange_tint</item>   
  6.       <item name="android:windowBackground">@color/orange_tint</item>   
  7.     </style>   
  8.        
  9.     <style name="OrangeAlertDialogTheme" parent="OrangeDialogTheme">   
  10.        <item name="android:windowBackground">@color/transparent</item>   
  11.     </style>   
  12.        
  13.        
  14.     <!-- Red -->      
  15.    <style name="RedDialogTheme" parent="@android:style/Theme.Dialog">   
  16.        <item name="android:tint">@color/red_tint</item>   
  17.    </style>   
  18.        
  19.     <style name="RedAlertDialogTheme" parent="RedDialogTheme">   
  20.        <item name="android:windowBackground">@color/magenta_tint</item>   
  21.     </style>   
  22.  </resources>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值