Prechádzať zdrojové kódy

Added linkColor() to Builder and md_link_color global theming attribute. Resolves https://github.com/afollestad/material-dialogs/issues/811

Aidan Follestad 9 rokov pred
rodič
commit
12c3a9c97c

+ 6 - 0
README.md

@@ -597,6 +597,7 @@ Pretty much every aspect of a dialog created with this library can be colored:
 new MaterialDialog.Builder(this)
         .titleColorRes(R.color.material_red_500)
         .contentColor(Color.WHITE) // notice no 'res' postfix for literal color
+        .linkColorAttr(R.attr.my_link_color_attr)  // notice attr is used instead of none or res for attribute resolving
         .dividerColorRes(R.color.material_pink_500)
         .backgroundColorRes(R.color.material_blue_grey_800)
         .positiveColorRes(R.color.material_red_500)
@@ -734,6 +735,11 @@ you show from an Activity which has a theme containing any of these attributes:
     -->
     <item name="md_content_color">#9C27B0</item>
 
+    <!--
+        By default, the link color is derived from the colorAccent attribute 
+        of AppCompat or android:colorAccent attribute of the Material theme.
+    -->
+    <item name="md_link_color">#673AB7</item>
 
     <!--
         By default, the positive action text color is derived

+ 5 - 4
core/src/main/java/com/afollestad/materialdialogs/DialogInit.java

@@ -84,7 +84,7 @@ class DialogInit {
             DialogUtils.setBackgroundCompat(dialog.view, drawable);
         }
 
-        // Retrieve action button colors from theme attributes or the Builder
+        // Retrieve color theme attributes
         if (!builder.positiveColorSet)
             builder.positiveColor = DialogUtils.resolveActionTextColorStateList(builder.context, R.attr.md_positive_color, builder.positiveColor);
         if (!builder.neutralColorSet)
@@ -93,6 +93,8 @@ class DialogInit {
             builder.negativeColor = DialogUtils.resolveActionTextColorStateList(builder.context, R.attr.md_negative_color, builder.negativeColor);
         if (!builder.widgetColorSet)
             builder.widgetColor = DialogUtils.resolveColor(builder.context, R.attr.md_widget_color, builder.widgetColor);
+        if (builder.linkColor == null)
+            builder.linkColor = DialogUtils.resolveActionTextColorStateList(builder.context, R.attr.md_link_color, builder.linkColor);
 
         // Retrieve default title/content colors
         if (!builder.titleColorSet) {
@@ -200,10 +202,9 @@ class DialogInit {
             dialog.content.setMovementMethod(new LinkMovementMethod());
             dialog.setTypeface(dialog.content, builder.regularFont);
             dialog.content.setLineSpacing(0f, builder.contentLineSpacingMultiplier);
-            if (builder.positiveColor == null)
+            if (builder.linkColor == null)
                 dialog.content.setLinkTextColor(DialogUtils.resolveColor(dialog.getContext(), android.R.attr.textColorPrimary));
-            else
-                dialog.content.setLinkTextColor(builder.positiveColor);
+            else dialog.content.setLinkTextColor(builder.linkColor);
             dialog.content.setTextColor(builder.contentColor);
             dialog.content.setGravity(builder.contentGravity.getGravityInt());
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {

+ 24 - 3
core/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -389,6 +389,7 @@ public class MaterialDialog extends DialogBase implements
         protected ColorStateList positiveColor;
         protected ColorStateList negativeColor;
         protected ColorStateList neutralColor;
+        protected ColorStateList linkColor;
         protected ButtonCallback callback;
         protected SingleButtonCallback onPositiveCallback;
         protected SingleButtonCallback onNegativeCallback;
@@ -485,6 +486,7 @@ public class MaterialDialog extends DialogBase implements
             this.positiveColor = DialogUtils.getActionTextStateList(context, this.widgetColor);
             this.negativeColor = DialogUtils.getActionTextStateList(context, this.widgetColor);
             this.neutralColor = DialogUtils.getActionTextStateList(context, this.widgetColor);
+            this.linkColor = DialogUtils.getActionTextStateList(context, this.widgetColor);
 
             int fallback = 0;
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
@@ -565,6 +567,8 @@ public class MaterialDialog extends DialogBase implements
                 this.btnSelectorNegative = s.btnSelectorNegative;
             if (s.widgetColor != 0)
                 this.widgetColor = s.widgetColor;
+            if (s.linkColor != null)
+                this.linkColor = s.linkColor;
             this.titleGravity = s.titleGravity;
             this.contentGravity = s.contentGravity;
             this.btnStackedGravity = s.btnStackedGravity;
@@ -863,7 +867,7 @@ public class MaterialDialog extends DialogBase implements
             return positiveColor(DialogUtils.resolveActionTextColorStateList(this.context, colorAttr, null));
         }
 
-        public Builder positiveColor(ColorStateList colorStateList) {
+        public Builder positiveColor(@NonNull ColorStateList colorStateList) {
             this.positiveColor = colorStateList;
             this.positiveColorSet = true;
             return this;
@@ -891,7 +895,7 @@ public class MaterialDialog extends DialogBase implements
             return negativeColor(DialogUtils.resolveActionTextColorStateList(this.context, colorAttr, null));
         }
 
-        public Builder negativeColor(ColorStateList colorStateList) {
+        public Builder negativeColor(@NonNull ColorStateList colorStateList) {
             this.negativeColor = colorStateList;
             this.negativeColorSet = true;
             return this;
@@ -919,12 +923,29 @@ public class MaterialDialog extends DialogBase implements
             return neutralColor(DialogUtils.resolveActionTextColorStateList(this.context, colorAttr, null));
         }
 
-        public Builder neutralColor(ColorStateList colorStateList) {
+        public Builder neutralColor(@NonNull ColorStateList colorStateList) {
             this.neutralColor = colorStateList;
             this.neutralColorSet = true;
             return this;
         }
 
+        public Builder linkColor(@ColorInt int color) {
+            return linkColor(DialogUtils.getActionTextStateList(context, color));
+        }
+
+        public Builder linkColorRes(@ColorRes int colorRes) {
+            return linkColor(DialogUtils.getActionTextColorStateList(this.context, colorRes));
+        }
+
+        public Builder linkColorAttr(@AttrRes int colorAttr) {
+            return linkColor(DialogUtils.resolveActionTextColorStateList(this.context, colorAttr, null));
+        }
+
+        public Builder linkColor(@NonNull ColorStateList colorStateList) {
+            this.linkColor = colorStateList;
+            return this;
+        }
+
         public Builder listSelector(@DrawableRes int selectorRes) {
             this.listSelector = selectorRes;
             return this;

+ 2 - 0
core/src/main/java/com/afollestad/materialdialogs/internal/ThemeSingleton.java

@@ -44,6 +44,8 @@ public class ThemeSingleton {
     public int backgroundColor = 0;
     @ColorInt
     public int dividerColor = 0;
+    @ColorInt
+    public ColorStateList linkColor = null;
 
     @DrawableRes
     public int listSelector = 0;

+ 1 - 0
core/src/main/res/values/attrs.xml

@@ -14,6 +14,7 @@
 
     <attr name="md_title_color" format="color" />
     <attr name="md_content_color" format="color" />
+    <attr name="md_link_color" format="color" />
 
     <attr name="md_positive_color" format="color" />
     <attr name="md_neutral_color" format="color" />