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. im和音视频开发哪个更好_找时间成为更好的开发人员
  2. 平面广告设计和Web设计的差别
  3. NOD32升级ID自动填写工具+更新版1.754
  4. Django CVE-2019-14234
  5. linux got分析,聊聊Linux动态链接中的PLT和GOT(3)——公共GOT表项
  6. 给公司部门设计的SOA架构
  7. 在windows下配置PostgreSQL
  8. 外媒:伊朗政府封锁加密通讯应用Signal
  9. oppor15android10怎么降级,OPPO R9S7.1系统怎么降回6.0版本 OPPO R9S7.1系统降级教程
  10. Spring Boot工程在IDEA中运行报错
  11. 常见的C++关键字有哪些?
  12. JAVA JDK 、Maven、IDEA安装
  13. exls表格搜索快捷键_excel表格查找快捷键|excel表格的常用功能快捷键介绍
  14. 局域网网络流量监控_网工必知:用于监控企业网络的10款最佳工具,拿走!不谢...
  15. 节选自周国平《风中的纸屑》里的一段话
  16. USBWebserver(网站架设工具)
  17. python代码画樱花主要特色,手机python代码画樱花
  18. 防火墙对计算机有作用吗,win7自带防火墙有用吗?关闭win7系统自带的防火墙对电脑有影响吗...
  19. 力扣-167题 两数之和 II - 输入有序数组(C++)- 双指针
  20. lv9-ARM体系结构与接口技术(1) 计算机硬件基础

热门文章

  1. emby无法播放服务器位置,解决emby不能播放部分视频一例
  2. 学生成绩查询(查最大值,最小值,平均值,升序,降序)
  3. unity 阳光插件_StarFilter Pro下载-StarFilter Pro(PS滤镜插件)v2020免费版
  4. 常见的前端安全问题(xss / csrf / sql / ddos / cdn...)
  5. 杂题题解(康复训练)
  6. 考勤工具如何一键生成
  7. 用C#读取XML指定节点下的值
  8. APP 开发软件为什么价格相差非常大?
  9. 考研政治全年复习规划
  10. Mininet环境搭建