修改dialog背景以及代码扩展AlertDialog和tint的使用

styles.xml

<style name="NewBorderDialogTheme" parent="android:Theme.Dialog"> 
 <item name="android:windowBackground">@drawable/your_drawable/item>  
</style> 

 

import android.app.AlertDialog;
import android.content.Context;

import android.content.res.TypedArray;
import android.content.res.Resources.Theme;

import  android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;

import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;


public class MyColorDialog  extends AlertDialog{
	 private static int NONE = -1;
     private int tint = NONE;
  
     /**
      * @param context
     * @param theme
     */
      protected MyColorDialog(Context context) {
        super(context);
        init();
       
    }
   
   /**
    * @param context
    * @param theme
     */
   protected MyColorDialog(Context context, int theme) {
       super(context, theme);
      init();
    }
    
    /**
     * 
    */
    private void init() {      
      final Theme theme = getContext().getTheme();
       final TypedArray attrs = theme.obtainStyledAttributes(new int[] { android.R.attr.tint });
      tint = attrs.getColor(0, NONE);
   }

	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
		 setTint(tint);
	}

	public void setTint(int tint) {
		// TODO Auto-generated method stub
		 this.tint = tint;
		 android.graphics.PorterDuff.Mode mode = PorterDuff.Mode.SRC_ATOP;
		 
       
            final Drawable d =  this.getWindow().getDecorView().getBackground();
          
               d.mutate().setColorFilter(tint, mode);
          
	      	    }
    

    /**
     * @param button
     */
    public void setCancelButton(Button button) {
       button.setOnClickListener(new View.OnClickListener() {
 
          @Override
         public void onClick(View v) {
            cancel();
          }
    
       });
    }
    
    /**
     * @param button
     */
    public void setPositiveButton(Button button) {
       button.setOnClickListener(new View.OnClickListener() {
 
          @Override
          public void onClick(View v) {
             dismiss();
          }
    
       });
    }
 
 
    
    public static class Builder extends AlertDialog.Builder {

       private MyColorDialog dialog;
      
       public Builder(Context context) {
          super(context);
          
          dialog = new MyColorDialog(context);
       }

       public Builder(Context context, int theme) {
          super(context);
          dialog = new MyColorDialog(context, theme);
       }
       
      

	@Override
       public MyColorDialog create() {
          return dialog;
       }
 
       @Override
       public Builder setMessage(CharSequence message) {
          dialog.setMessage(message);
          return this;
       }
 
       @Override
       public Builder setTitle(CharSequence title) {
          dialog.setTitle(title);
          return this;
       }
 
       @Override
       public Builder setPositiveButton(CharSequence text,
             OnClickListener listener) {
          dialog.setButton(BUTTON_POSITIVE, text, listener);
          return this;
       }
 
          @Override
          public Builder setIcon(int iconId) {
             dialog.setIcon(iconId);
             return this;
          }
 
    }


	

}

 使用

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

 

<?xml version="1.0" encoding="utf-8"?>
 <resources>
    <color name="red_tint">#88FF0000</color>
    <color name="blue_tint">#880000FF</color>
    <color name="yellow_tint">#88FFFF00</color>
    <color name="purple_tint">#88995f86</color>
    <color name="orange_tint">#aaffbf00</color>
    <color name="magenta_tint">#88ff33cc</color>
    <color name="transparent">#0000</color>
 </resources> 

 

<?xml version="1.0" encoding="utf-8"?>
 <resources>
    <!-- Orange -->
    <style name="OrangeDialogTheme" parent="@android:style/Theme.Dialog">
      <item name="android:tint">@color/orange_tint</item>
      <item name="android:windowBackground">@color/orange_tint</item>
    </style>
    
    <style name="OrangeAlertDialogTheme" parent="OrangeDialogTheme">
       <item name="android:windowBackground">@color/transparent</item>
    </style>
    
    
    <!-- Red -->   
   <style name="RedDialogTheme" parent="@android:style/Theme.Dialog">
       <item name="android:tint">@color/red_tint</item>
   </style>
    
    <style name="RedAlertDialogTheme" parent="RedDialogTheme">
       <item name="android:windowBackground">@color/magenta_tint</item>
    </style>
 </resources>

 

你可能感兴趣的:(android,xml)