android - 无法将形状应用于按钮

标签 android android-layout button

我有一个奇怪的问题。我创建了一个简单的 Button看起来像这样:

<com.google.android.material.button.MaterialButton
android:id="@+id/continue_button"
android:background="@drawable/green_button_selector"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/landing_margin"
android:layout_marginEnd="@dimen/landing_margin"
android:layout_marginBottom="@dimen/landing_margin"
android:text="@string/login_button_text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

我想做Button绿色带圆角,所以我创建了一个名为 green_button_selector 的 xml 文件并设置为按钮背景。此文件代码发布在下面
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:shape="rectangle">
    <solid android:color="@color/colorGreenButton" />
    <corners android:radius="10dp" />
</shape>

但是我的按钮没有变成绿色,而是我的应用程序的 colorAccent,任何想法我做错了什么?

最佳答案

如果您使用的是 Material 按钮,则无需创建自定义可绘制对象。

<com.google.android.material.button.MaterialButton
    style="@style/Widget.MaterialComponents.Button"
    android:textAppearance="@style/TextAppearance.AppCompat.Small"
    app:backgroundTint="@android:color/holo_green_dark"
    app:cornerRadius="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:text="BUTTON" />

enter image description here

更多:https://material.io/develop/android/components/material-button/

关于android - 无法将形状应用于按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53168301/

相关文章:

c++ - 按钮 gtkmm-3.0 的右键单击信号

android - 如何将按钮真正放在底部

Java - 当用户按下按钮时暂停 for 循环并恢复迭代

android - 已收到 GCM 通知但未显示

Java import com.google.common.io.Files 无法解析

android - ScrollView distrubing ListView inside LinearLayout

android - 如何在约束布局中创建水平 ScrollView

Android 资源编译失败(非转义字符)- 即使我修复了错误

java - 使用来自 URL 的图像创建 GridView

android - 如何在警报对话框中扩充包含 ListView 的布局?