Android UI开发——Material Design界面设计风格

添加依赖:
implementation 'com.android.support:design:30.0.3'

使用:

    <com.google.android.material.button.MaterialButton

        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:insetLeft="50dp"
        android:insetTop="5dp"
        android:insetRight="50dp"
        android:insetBottom="5dp"
        android:text="确认办理"
        android:textColor="#ffffffff"
        app:strokeColor="#000000"
        app:strokeWidth="2dp"
        android:textSize="24sp"
        android:theme="@style/Theme.MaterialComponents.Light.DarkActionBar"
        android:visibility="visible"
        app:backgroundTint="#FFA54C" />

MaterialButton继承AppCompatButton,在原来Button的基础上做了一些扩展,如圆角、描边、前置和后置icon(icon支持设置Size、Tint、Padding、Gravity等),还支持按压水波纹并且设置color,基本能满足日常的需求。

公开属性如下:

属性                            描述
app:backgroundTint                  背景着色
app:backgroundTintMode              着色模式
app:strokeColor                     描边颜色
app:strokeWidth                     描边宽度
app:cornerRadius                    圆角大小
app:rippleColor                     按压水波纹颜色
app:icon                            图标icon
app:iconSize                        图标大小
app:iconGravity                     图标重心
app:iconTint                        图标着色
app:iconTintMode                    图标着色模式
app:iconPadding                     图标和文本之间的间距


关于background
在1.2版本以前,MaterialButton只能通过app:backgroundTint属性设置背景色,该属性接收color state list。不能通过android:background设置自定义drawable。
1.2版本后,官方已修复此问题。如果未设置自定义背景,则 MaterialShapeDrawable 仍将用作默认背景。
也就是说,如果按钮背景是纯色,可以通过app:backgroundTint指定;如果按钮背景是渐变色,则需要自己定义drawable,然后通过android:background设置。
注意:如果要使用android:background设置背景,则需要将backgroundTint设置为@empty,否则background不会生效。
 

<com.google.android.material.button.MaterialButton
    android:background=”@drawable/custom_background”
    app:backgroundTint=”@empty” />

指定@empty后,Android Studio会出现红色警告,可以正常运行,忽略就好。不过既然已经自定义drawable,就没必要使用MaterialButton,直接用普通的Button甚至用TextView就好了。

关于阴影:

MD组件默认都是自带阴影的,MaterialButton也不例外。但是有时候我们并不想要按钮有阴影,那么这时候可以指定style为
style="@style/Widget.MaterialComponents.Button.UnelevatedButton",这样就能去掉阴影,让视图看起来扁平化。

关于theme


在MDC1.1.0以后,使用MaterialButton可能会出现闪退的问题,原因就是使用了MD控件,但是未将them设置为MaterialComponents。解决方法可以有几种:

先在style.xml自定义MaterialComponents_Theme

 <style name="MaterialComponents_Theme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- Customize your theme here. -->
        ...
    </style>

方法一:
AndroidManifest里application节点下配置,作用域为整个应用

<application
        ...
        android:theme="@style/MaterialComponents_Theme"

方法二:
只在当前activity配置,作用域为当前activity

<activity
        ...
        android:theme="@style/MaterialComponents_Theme"

方法三:
为每个在使用到MD控件的地方配置,作用域只针对当前控件

<com.google.android.material.button.MaterialButton
	...
    android:theme="@style/Theme.MaterialComponents.Light.NoActionBar" />

参考地址:

https://blog.csdn.net/magic0908/article/details/101029876


版权声明:本文为hdhhd原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。