【问题标题】:Can not apply shape to button无法将形状应用于按钮
【发布时间】:2019-04-09 15:12:23
【问题描述】:

我有一个奇怪的问题。我创建了一个简单的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,任何想法我做错了什么?

【问题讨论】:

  • 重新构建您的项目,当您新建可绘制时,它不会立即添加。
  • 我试过了,可惜没有效果
  • 你的drawable正在工作,我认为你的约束布局有问题。尝试设置宽度并检查。
  • 我认为边距和宽度会造成问题。
  • 您找到解决方案了吗?

标签: android android-layout button


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:
    <com.google.android.material.button.MaterialButton
    android:id="@+id/material_text_button"
    style="@style/Widget.MaterialComponents.Button.OutlinedButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/outlined_button_label_enabled"/>
    

    你可以在下面的属性相同。

    app:cornerRadius app:backgroundTint

    阅读更多来自 Material Button official document

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-13
      • 2018-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-17
      • 2017-01-15
      相关资源
      最近更新 更多