简体   繁体   English

Android XML 如何在自定义形状按钮内设置背景渐变

[英]Android XML How to set background gradient inside a custom shape button

I am creating a button shape look like WWTBAM game show:我正在创建一个看起来像 WWTBAM 游戏节目的按钮形状:

在此处输入图像描述

I have created the shape for it but don't know how to put the gradient in it.我已经为它创建了形状,但不知道如何将渐变放入其中。

This is my styles.xml file I created:这是我创建的 styles.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="ButtonShapeArrow">
        <item name="cornerFamily">cut</item>
        <item name="cornerSize">50%</item>
    </style>
</resources>

And button in layout_main.xml: layout_main.xml 中的按钮:

<com.google.android.material.button.MaterialButton
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Start"
    app:shapeAppearance="@style/ButtonShapeArrow"/>

How do I set gradient for that button?如何为该按钮设置渐变?

Create a custom background, you can change angle to change to position of the gradient创建自定义背景,可以改变angle改变为position的渐变

bg_gradient.xml bg_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="270"
         android:endColor="@android:color/transparent"
         android:startColor="#802BC0E4" />
</shape>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM