Android MaterialButton的一些问题

MaterialButton和MaterialCardView的都新增了边框属性,我们没必要为了一个边框写那么多shape,一旦多了谁着得住。

1、在使用MaterialButton注意一点是它必须设置android:textAppearance属性,不然会崩溃

This component requires that you specify a valid TextAppearance attribute. Update your app theme to inherit from Theme.MaterialComponents (or a descendant)

 public MaterialButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray attributes = ThemeEnforcement.obtainStyledAttributes(context, attrs, styleable.MaterialButton, defStyleAttr, style.Widget_MaterialComponents_Button, new int[0]);
        this.iconPadding = attributes.getDimensionPixelSize(styleable.MaterialButton_iconPadding, 0);
        this.iconTintMode = ViewUtils.parseTintMode(attributes.getInt(styleable.MaterialButton_iconTintMode, -1), Mode.SRC_IN);
        this.iconTint = MaterialResources.getColorStateList(this.getContext(), attributes, styleable.MaterialButton_iconTint);
        this.icon = MaterialResources.getDrawable(this.getContext(), attributes, styleable.MaterialButton_icon);
        this.iconGravity = attributes.getInteger(styleable.MaterialButton_iconGravity, 1);
        this.iconSize = attributes.getDimensionPixelSize(styleable.MaterialButton_iconSize, 0);
        this.materialButtonHelper = new MaterialButtonHelper(this);
        this.materialButtonHelper.loadFromAttributes(attributes);
        attributes.recycle();
        this.setCompoundDrawablePadding(this.iconPadding);
        this.updateIcon();
    }

 

private static void checkTextAppearance(Context context, AttributeSet set, @StyleableRes int[] attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes, @StyleableRes int... textAppearanceResIndices) {
        TypedArray themeEnforcementAttrs = context.obtainStyledAttributes(set, styleable.ThemeEnforcement, defStyleAttr, defStyleRes);
        boolean enforceTextAppearance = themeEnforcementAttrs.getBoolean(styleable.ThemeEnforcement_enforceTextAppearance, false);
        if (!enforceTextAppearance) {
            themeEnforcementAttrs.recycle();
        } else {
            boolean validTextAppearance;
            if (textAppearanceResIndices != null && textAppearanceResIndices.length != 0) {
                validTextAppearance = isCustomTextAppearanceValid(context, set, attrs, defStyleAttr, defStyleRes, textAppearanceResIndices);
            } else {
                validTextAppearance = themeEnforcementAttrs.getResourceId(styleable.ThemeEnforcement_android_textAppearance, -1) != -1;
            }

            themeEnforcementAttrs.recycle();
            if (!validTextAppearance) {
                throw new IllegalArgumentException("This component requires that you specify a valid TextAppearance attribute. Update your app theme to inherit from Theme.MaterialComponents (or a descendant).");
            }
        }
    }

 它会检查textApearance属性,解决方式有两种如下

 

1、添加它就好

android:textAppearance="?android:attr/textAppearanceButton"

2、application或activity或控件的theme继承自Theme.MaterialComponents.xxxx,使type能够找到这个属性,如

 

其实想了想,也许以后MaterialComponents的text相关组件都会进行这种操作吧。

2、在使用MaterialButton时候可能遇到背景颜色不能充满控件的问题。

如果按照以前默认的方式添加背景颜色,我们发现背景颜色不能充满上下编剧,我们对比使用appcompatButton



        

Android MaterialButton的一些问题_第1张图片

1、从上面的图片可以看出它们的背景颜色不一样。MaterialButton是不受android:background控制的,官方建议我们设置app:backgroundHint来进行背景的更改。

2、AppCompatButton如果设置了android:background会覆盖上下左右的间距,MaterialButton则不会

3、MaterialButton设置了app:backgroundHint左右是没有间距的,而上下有。AppCompatButton设置了app:backgroundHint是上下左右都有间距

找到原因https://github.com/material-components/material-components-android/blob/master/docs/components/MaterialButton.md#attributes

Note: MaterialButton is visually different from Button and AppCompatButton. One of the main differences is that AppCompatButton has a 4dp inset on the left and right sides, whereas MaterialButton does not. To add an inset to match AppCompatButton, set android:insetLeft and android:insetRight on the button to 4dp, or change the spacing on the button's parent layout.

反正AppCompatButton左右留了4个dp的占位,而MaterialButton没有。

试验

Android MaterialButton的一些问题_第2张图片

通过设置上下左右inset控制button绘制的范围,背景颜色也能控制。那么我们就解决了这个问题了

方案:

设置insetTop和insetBottom为0dp

 

你可能感兴趣的:(Android控件小节)