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能够找到这个属性,如

 <com.google.android.material.button.MaterialButton
            android:id="@+id/btn_ok"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:gravity="center"
            android:text="确认办理"
            android:textColor="#ffffffff"
            android:textSize="24sp"
            android:visibility="visible"
            app:backgroundTint="#FFA54C"
            android:theme="@style/Theme.MaterialComponents.Light.DarkActionBar"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

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

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

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

<androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btn_cancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:gravity="center"
            android:text="确认办理"
            android:textColor="#ffffffff"
            android:textSize="24sp"
            android:theme="@style/Theme.MaterialComponents.Light"
            android:visibility="visible"
            android:background="#FFA54C"
            
            app:layout_constraintBottom_toTopOf="@id/btn_ok"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/btn_ok"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:gravity="center"
            android:text="确认办理"
            android:textColor="#ffffffff"
            android:textSize="24sp"
            android:theme="@style/Theme.MaterialComponents.Light"
            android:visibility="visible"
            android:background="#FFA54C"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

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没有。

试验

<com.google.android.material.button.MaterialButton
            android:id="@+id/btn_ok"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:gravity="center"
            android:padding="5dp"
            app:cornerRadius="0dp"
            android:insetLeft="50dp"
            android:insetTop="0dp"
            android:insetBottom="0dp"
            android:insetRight="50dp"
            android:textAppearance="@style/Widget.MaterialComponents.Button"
            android:text="确认办理"
            android:textColor="#ffffffff"
            android:textSize="24sp"
            android:visibility="visible"
            app:backgroundTint="#FFA54C"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>

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

方案:

设置insetTop和insetBottom为0dp

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值