修改dialog背景以及代码扩展AlertDialog和tint的使用
16lz
2021-01-23
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); }@Overridepublic void show() {// TODO Auto-generated method stubsuper.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>
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer