我目前正在android studio 中从事一个项目。在该应用程序中,所有活动都有按钮。在
MainActivity
(应用程序的主菜单)中有3个按钮。我创建了一个名为 rounded 的 drawable 文件。它用于按钮的背景。以下代码位于 rounded
文件中。这会将按钮变成圆形按钮。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#9AFD36"
android:centerColor="#8CE63E"
android:endColor="@color/teal_700"
android:angle="180"
android:type="linear"/>
<corners android:radius="10000dp"/>
</shape>
我将此背景添加到这样的按钮中,
<Button
android:id="@+id/areabtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Surface Area"
android:layout_centerInParent="true"
android:background="@drawable/rounded"
android:textColor="@color/black"/>
在第 7 行中,我为该文件设置了背景,按钮将在那里显示该渐变,但是当我运行它时,它会变成颜色文件中的 Primary Color。颜色文件中有以下颜色,(原色是
purple_500
)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#11CC6B</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
</resources>
这位于值文件夹中
themes
文件的代码中。
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.MathematiciansCalculator" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
我还在其他活动中为后退按钮设置了另一个渐变,但它们也不显示给定的背景。
Android Studio 版本:- Android Studio Bumblebee | 2021年1月1日金丝雀11
当您共享您的应用程序主题时父级是Theme.MaterialComponents.DayNight.DarkActionBar
因此您的按钮行为类似于 MaterialButton,因此请将 backgroundTint 设置为“null”
使用 MaterialButton 制作圆角
<com.google.android.material.button.MaterialButton
android:id="@+id/button_share"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="0dp"
android:layout_height="56dp"
android:layout_gravity="center"
android:layout_weight="1"
android:backgroundTint="@color/colorCard"
android:fontFamily="@font/medium"
android:insetLeft="0dp"
android:insetTop="0dp"
android:insetRight="0dp"
android:insetBottom="0dp"
android:text="Share"
android:textAllCaps="true"
android:textColor="@color/colorText"
android:textSize="18sp"
app:cornerRadius="28dp" />
我在@KGeeks 的帮助下找到了我的问题的答案。正如她所说,它需要
app:backgroundTint="@null"
线。
您好@RedDragon,现有答案的要点很好,但不完整,或者在我看来没有正确解决您的问题:
app:backgroundTint="@null"
将禁用 MaterialButton
的现有背景颜色,或者如果您输入一个值而不是 null
,则更改它。
提示:您还可以使用
app:background="@empty"
但要提到的重要一点是
MaterialButton
不支持渐变,仅支持单色。
简短的实地考察,了解为什么使用 Tint 而不是文档中的 android:background
:
请勿使用
属性。android:background
管理自己的背景可绘制对象,并设置新的背景 意味着MaterialButton
不能再保证新属性 它介绍将正常运行。如果默认背景是 更改后,MaterialButton
无法保证明确定义的行为。MaterialButton
正如您所看到的,它与 Android 中的标准小部件非常不同,
MaterialDesign
是一个库。
现在使用按钮渐变的解决方案如下:
如果您使用 Material Theme 来应用背景渐变,请使用
androidx.appcompat.widget.AppCompatButton
,因为如前所述,MaterialButton
尚不提供支持。在那里您可以将 drawable
设置为 android:background
属性或使用 @style
。
使用 app:backgroundTint="@color/buttonColor" 而不是 android:backgroundTint="@color/buttonColor"