我对material 设计的按钮样式感到困惑.我想得到彩色的凸起按钮,就像附件中的链接一样.,如"用法"部分下的"强制停止"和"卸载"按钮.有可用的样式吗?或者我需要定义它们吗?

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"

所有的风格都消失了,比如touch 动画、阴影、圆角等等.

推荐答案

你可以使用

在您的build.gradle基础上再加the dependency:

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

你可以在这里查full documentationAPI here.

要更改background color,您有两个 Select .

  1. 使用100属性.

类似于:

<style name="MyButtonStyle"
 parent="Widget.MaterialComponents.Button">
    <item name="backgroundTint">@color/button_selector</item>
    //..
</style>
  1. 在我看来,这将是最好的 Select .如果您想覆盖默认样式中的一些主题属性,那么您可以使用新的100属性.

类似于:

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

enter image description hereenter image description here

您可以使用以下样式之一:

  • Filled Button (default): style="@style/Widget.MaterialComponents.Button
  • Text Button: style="@style/Widget.MaterialComponents.Button.TextButton"
  • OutlinedButton:style="@style/Widget.MaterialComponents.Button.OutlinedButton"

OLD Support Library:

有了新的Support Library 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" />

默认情况下,这个类将使用主题的accent colour作为按钮的填充背景 colored颜色 ,同时使用白色作为按钮的文本 colored颜色 .

您可以使用以下属性自定义按钮:

  • app:rippleColor: 要用于按钮涟漪效果的 colored颜色

  • app:backgroundTint: 用于将色调应用于按钮的背景.如果要更改按钮的背景色,请使用此属性而不是背景.

  • app:strokeColor:用于按钮笔划的 colored颜色

  • app:strokeWidth: 用于按钮笔划的宽度

  • app:cornerRadius: ,用于定义按钮拐角使用的半径

Android相关问答推荐

在Kotlin Jetpack Compose中点击按钮后启动另一个Android应用程序

Jetpack Compose中的导航找不到NavHost类的名称为:startDestination";的参数

如何在Jetpack导航中不显示目的地?

FireBase Android ChildEventListener在被规则拒绝时触发(RTDB)

不能有意地从一个活动的可组合功能转移到另一个活动

从片段导航回来

Jetpack Compose X.dp 性能问题?

是否可以在 compose 中使用三次贝塞尔曲线进行动画?

Android - 如何使 TextInputEditText 的高度恰好为 2 行?

我如何比较多个时间范围并在 Android Compose 中并排显示它们

根据另一个数组的值对数组进行排序

在 Android Studio 中获取更新版本的 Picasso 库的错误警告消息

为什么项目捕获对象给我在 Compose 中找不到参考

组成不重叠的元素

MediumTopAppBar Material3 只更改大标题

使用默认使用 RTL 语言的项目本地化 android 应用程序

插入查询室 OnConflictStrategy.REPLACE

并行运行两个挂起函数并在第一个返回时返回

如何从 Jetpack Compose 中的 Radio 组中获取价值

在 Android 10 (API 29) 中隐藏状态栏并在应用程序中使用其空间