我对材质设计的纽扣款式很困惑。我想要像附件链接那样的彩色凸起按钮。,比如用法部分下面的“强制停止”和“卸载”按钮。是否有可用的样式或者我需要定义它们?

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.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/

一个用于创建新材质按钮的方便类。 类中的按钮的更新的材质样式 构造函数。小部件将显示正确的默认材质 不使用样式标志的样式。

其他回答

您可以使用 材料组件库。

将依赖项添加到build.gradle:

dependencies { implementation ‘com.google.android.material:material:1.3.0’ }

然后添加MaterialButton到你的布局:

<com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.OutlinedButton" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        app:strokeColor="@color/colorAccent"
        app:strokeWidth="6dp"
        app:layout_constraintStart_toStartOf="parent"
        app:shapeAppearance="@style/MyShapeAppearance"
   />

你可以在这里查看完整的文档和API。

要改变背景颜色,你有两个选项。

使用backgroundTint属性。

喜欢的东西:

<style name="MyButtonStyle"
 parent="Widget.MaterialComponents.Button">
    <item name="backgroundTint">@color/button_selector</item>
    //..
</style>

在我看来,这是最好的选择。如果你想覆盖一些默认样式的主题属性,那么你可以使用新的materialThemeOverlay属性。

喜欢的东西:

<style name="MyButtonStyle"
 parent="Widget.MaterialComponents.Button">
   <item name=“materialThemeOverlay”>@style/GreenButtonThemeOverlay</item>
</style>

<style name="GreenButtonThemeOverlay">
  <!-- For filled buttons, your theme's colorPrimary provides the default background color of the component --> 
  <item name="colorPrimary">@color/green</item>
</style>

选项2至少要求版本为1.1.0。

你可以使用下列样式之一:

填充按钮(默认):style="@style/Widget.MaterialComponents.Button . 文本按钮:style="@style/Widget.MaterialComponents.Button.TextButton" OutlinedButton:风格= " @style / Widget.MaterialComponents.Button.OutlinedButton”


旧支持库:

在新的支持库28.0.0中,设计库现在包含了MaterialButton。

你可以添加这个按钮到我们的布局文件:

<android.support.design.button.MaterialButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="YOUR TEXT"
    android:textSize="18sp"
    app:icon="@drawable/ic_android_white_24dp" />

默认情况下,这个类将使用主题的强调色作为按钮填充的背景色,并使用白色作为按钮的文本颜色。

你可以用这些属性自定义按钮:

app:rippleColor:用于按钮涟漪效果的颜色 app:backgroundTint:用于为按钮的背景添加色调。如果您希望更改按钮的背景颜色,请使用此属性而不是背景。 app:strokeColor:用于按钮描边的颜色 app:strokeWidth:用于按钮描边的宽度 app:cornerRadius:用于定义按钮边角的半径

我是这样得到我想要的东西的。

首先,创建一个按钮(在styles.xml中):

<style name="Button">
    <item name="android:textColor">@color/white</item>
    <item name="android:padding">0dp</item>
    <item name="android:minWidth">88dp</item>
    <item name="android:minHeight">36dp</item>
    <item name="android:layout_margin">3dp</item>
    <item name="android:elevation">1dp</item>
    <item name="android:translationZ">1dp</item>
    <item name="android:background">@drawable/primary_round</item>
</style>

按钮的波纹和背景,作为一个可绘制的primary_round.xml:

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/primary_600">
  <item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <corners android:radius="1dp" />
        <solid android:color="@color/primary" />
    </shape>
  </item>
</ripple>

这增加了我正在寻找的连锁反应。

你可以通过添加z轴来给视图添加航空,并且可以有默认的阴影。该功能在L预览版中提供,并将在发布后可用。现在,你可以简单地添加一个图像给这个按钮的背景

简单的解决方案


步骤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库,它允许您轻松地修改按钮颜色和波纹颜色

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"/>

你不需要为每个你想要不同颜色的按钮创建一个样式,允许你随机自定义颜色