我对材质设计的纽扣款式很困惑。我想要像附件链接那样的彩色凸起按钮。,比如用法部分下面的“强制停止”和“卸载”按钮。是否有可用的样式或者我需要定义它们?
http://www.google.com/design/spec/components/buttons.html#buttons-usage
我找不到默认的按钮样式。
例子:
<Button style="@style/PrimaryButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"
android:id="@+id/button3"
android:layout_below="@+id/editText5"
android:layout_alignEnd="@+id/editText5"
android:enabled="true" />
如果我尝试通过添加来改变按钮的背景颜色
android:background="@color/primary"
所有的样式都消失了,比如触摸动画、阴影、圆角等等。
我刚刚创建了一个android库,它允许您轻松地修改按钮颜色和波纹颜色
https://github.com/xgc1986/RippleButton
<com.xgc1986.ripplebutton.widget.RippleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="Android button modified in layout"
android:textColor="@android:color/white"
app:buttonColor="@android:color/black"
app:rippleColor="@android:color/white"/>
你不需要为每个你想要不同颜色的按钮创建一个样式,允许你随机自定义颜色
简单的解决方案
步骤1:使用最新的支持库
compile 'com.android.support:appcompat-v7:25.2.0'
步骤2:使用AppCompatActivity作为你的父Activity类
public class MainActivity extends AppCompatActivity
步骤3:在布局XML文件中使用app命名空间
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
第四步:使用AppCompatButton而不是Button
<android.support.v7.widget.AppCompatButton
android:id="@+id/buttonAwesome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Awesome Button"
android:textColor="@color/whatever_text_color_you_want"
app:backgroundTint="@color/whatever_background_color_you_want"/>
除了android.support.design.button.MaterialButton (Gabriele Mariotti提到过)之外,
还有另一个按钮小部件,名为com.google.android.material.button.MaterialButton,它有不同的风格,并从AppCompatButton扩展:
style="@style/Widget.MaterialComponents.Button"
style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
style="@style/Widget.MaterialComponents.Button.TextButton"
style="@style/Widget.MaterialComponents.Button.Icon"
style="@style/Widget.MaterialComponents.Button.TextButton.Icon"
填充,升高按钮(默认):
style="@style/Widget.MaterialComponents.Button"
填充,未抬高按钮:
style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
文本按钮:
style="@style/Widget.MaterialComponents.Button.TextButton"
图标按钮:
style="@style/Widget.MaterialComponents.Button.Icon"
app:icon="@drawable/icon_24px" // Icons can be added from this
带有图标的文本按钮::
阅读:https://material.io/develop/android/components/material-button/
一个用于创建新材质按钮的方便类。
类中的按钮的更新的材质样式
构造函数。小部件将显示正确的默认材质
不使用样式标志的样式。