我在drawable中创建了一个gradient_file.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="20dp" />
<gradient
android:startColor="@color/white"
android:endColor="@color/black"
android:angle="45"
android:type="linear" />
</shape>
Run Code Online (Sandbox Code Playgroud)
我已将其应用于布局中的按钮
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="100dp"
android:background="@drawable/gradient_file"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.243" />
Run Code Online (Sandbox Code Playgroud)
这是我得到的结果:
颜色应该不同。我已经重建了项目,但颜色在预览和模拟器中都没有改变。
小智 6
在 theme.xml 中,更改
<style name="Theme.YourApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
Run Code Online (Sandbox Code Playgroud)
到
<style name="Theme.YourApp" parent="Theme.AppCompat.DayNight.DarkActionBar">
Run Code Online (Sandbox Code Playgroud)
然后提交的绘图就可以工作了!
它被 theme.xml 文件中的颜色覆盖。转到您的 theme.xml 文件(在 res/values/themes 中)并替换
<item name="colorPrimary">@color/purple_500</item>
Run Code Online (Sandbox Code Playgroud)
和
<item name="colorPrimary">@null</item>
Run Code Online (Sandbox Code Playgroud)
查看您的渐变效果。
编辑:我不确定为什么这是......可能是较新的 android studio 版本中的错误?但我能够让你的按钮渐变与这个解决方案一起工作