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.MaterialButtonandroid: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.AppCompatButtonandroid: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.MaterialButtonandroid: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.MaterialButtonandroid: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

Android MaterialButton的一些问题相关推荐

  1. Android P:BottomAppBar和MaterialButton

    Android P Developer Preview is available for public now. In this tutorial, we'll be looking at two i ...

  2. 【yechaoa】5年Android开发的2021年终总结,实现Flag的一年

    前言 不知不觉一年又过去了,总是在回忆的时候感叹时间的流逝,回首这一年来的过往,也是不平凡的一年~ 女儿出生了,换了新工作,认识了新朋友,也有很多新体验. 生活 尝试新菜 2021年的第一天家庭聚餐, ...

  3. 【涨姿势】你没用过的BadgeDrawable

    1.前言 通常情况下,我们在做小红点效果的时候,会有两种选择: 自定义BadgeView,然后设置给目标View xml写一个View,然后设置shape 有的同学可能会想,能实现不就行了吗,是的,代 ...

  4. flutter 局部状态和全局状态区别_给 Android 开发者的 Flutter 指南

    这篇文档旨在帮助 Android 开发者利用既有的 Android 知识来通过 Flutter 开发移动应用.如果你了解 Android 框架的基本知识,你就可以使用这篇文档作为 Flutter 开发 ...

  5. [Material Design] 教你做一个Material风格、动画的button(MaterialButton)

    原创作品,转载请注明出处:http://blog.csdn.net/qiujuer/article/details/39831451 前段时间Android L 公布了,相信看过公布会了解过的朋友都为 ...

  6. Android 8.0 AutoFill自动填写框架实践

    1.构建表单界面 为 TextInputEditText 添加 android:importantForAutofill="yes" <?xml version=" ...

  7. android浮动按钮_Android扩展浮动操作按钮

    android浮动按钮 Extended Floating Action Button is the newly introduced class with Material Components l ...

  8. Android MaterialButtonToggleGroup

    In this tutorial, we'll be focusing on MaterialButtonToggleGroup, newly introduced with Android Mate ...

  9. Android 音乐APP(五)音乐通知栏、后台播放音乐

    Android 音乐通知栏 前言 正文 ① 通知栏按钮点击监听 ② 通知栏点击监听 ③ 通知栏业务处理 ④ 运行效果图 结语 前言   这篇文章的标题有些言简意赅了,也突出了这篇文章的核心,那就是通知 ...

最新文章

  1. android 左移动画_Android研究院之游戏开发Tween动画的实现(十九)
  2. Client Copy后的号码段重复
  3. ITK:通过指定区域裁剪图像
  4. linux查看日志命令_查看log日志基础命令
  5. android自定义趋势图
  6. wordpress各种获取url函数总结
  7. un8.21:用html实现增删改查功能(代码篇)。
  8. 【信号与系统|吴大正】4:信号分解、傅里叶变换与信号谱(下)
  9. Arch Linux下 让MPlayer用上CoreAVC1.7.0.0解码器
  10. 富文本编辑器在Java中使用
  11. 【状压DP】状态压缩动态规划入门超详解
  12. 操作系统-io控制器
  13. 软件测试之因果图法(P24——P26)
  14. 单片机复位电路的可靠性设计及精典实用复位电路
  15. 请控制好你的情绪--职场情绪管理
  16. 基于Spring Boot 2 和 Vue.js 2 的 食品科学与工程学院网站的设计与实现
  17. 纯干货分享,2021年阿里巴巴社招面试题总结,本人上周已成功入职!
  18. 机器视觉、模式识别库
  19. php中调用css设置表格,CSS表格设置实例
  20. 专心致志求精进——给自己的生日祝福

热门文章

  1. windows xp显示本地连接受限制,无法连接网络
  2. elementUi中的表单全部禁用allDisabled
  3. 什么是PHP以及PHP的特性
  4. [01-23][ dcwz电影合集][10部][18:30发布][亲测]
  5. [wp7游戏]wp7 [动作]+[冒险]系列游戏~~集合~~
  6. python 音乐编程,python插入音乐进行播放的方法
  7. 李笑来 糖果盛宴,免费领取
  8. [架构之路-171]-《软考-系统分析师》-5-数据库系统-4- 数 据 库 的 控 制 功 能(并发控制、性能优化)
  9. 打游戏的计算机需要多少钱,玩一天电脑游戏要耗费多少度电?其实没你想的那么夸张...
  10. 从400多k的大小减到了2B,我的APP是怎么优化的?