Android 如何为材质按钮设置渐变背景?
我目前正在使用这段代码,但是背景没有改变。它仍然显示强调色作为背景Android 如何为材质按钮设置渐变背景?,android,android-drawable,android-button,material-components-android,Android,Android Drawable,Android Button,Material Components Android,我目前正在使用这段代码,但是背景没有改变。它仍然显示强调色作为背景 <com.google.android.material.button.MaterialButton android:id="@+id/materialButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello" app:cornerRadiu
<com.google.android.material.button.MaterialButton
android:id="@+id/materialButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
app:cornerRadius="32dp"
android:background="@drawable/gradiant_blue"/>
gradiant_blue.xml
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="-45"
android:startColor="#2979FF"
android:endColor="#7C4DFF"/>
</shape>
我目前正在使用
材料组件版本:1.0.0-rc02
请阅读材料按钮的文档,从 上面说,, 对于填充的按钮,主题的colorPrimary提供组件的默认背景色,文本颜色为colorOnPrimary。对于未填充的按钮,主题的colorPrimary提供组件的默认文本颜色,默认情况下背景色是透明的 我想这就是你看到其他颜色(强调色)的原因,也请检查设置背景色的属性 提到了以下属性:
app:backgroundTint
app:backgroundTintMode
LE:从我的角度来看,我建议您使用按钮
或AppCompatButton
试试这个:
gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="0"
android:endColor="#027FEE"
android:startColor="#2CC6F2" />
</shape>
<!-- replace MaterialButton with Button or AppCompatButton -->
<com.google.android.material.button.MaterialButton
android:id="@+id/materialButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/gradient"
android:text="Hello" />
结果:
要设置背景,您需要扩展基本主题 Theme.AppCompat.Light.NoActionBar 而不是 Theme.MaterialComponents.Light.NoActionBar
要通过
MaterialButton
使用自定义可绘制背景,可以使用android:background
属性:
<MaterialButton
app:backgroundTint="@null"
android:background="@drawable/bkg_button_gradient"
... />
注意:至少需要版本
当前,添加app:backgroundTint=“@null”
非常重要,以避免默认情况下自定义背景不会使用backgroundTint
颜色着色
对于较低版本的材质组件库,您必须使用
AppCompatButton
什么是渐变蓝
?你应该把它贴出来code@Pavan你可以经历这一切。希望这有助于你我需要把梯度背景。背景色调不允许使用drawables。您使用的是哪个版本。我试过你的代码,但仍然不工作背景没有改变?你能试一下-“'implementation'com.google.android.material:material:1.0.0-rc02”没有错误,背景没有改变。它甚至在预览模式下不会改变,在实际设备中也不会改变。如果主题不从材质组件延伸,则“渐变”可以与其他视图和按钮配合使用,不能使用Material Buttoncom.google.android.Material.Button.MaterialButton
仅适用于materialComponents主题,而不适用于appcompat主题
<MaterialButton
app:backgroundTint="@null"
android:background="@drawable/bkg_button_gradient"
... />