【问题标题】:Sudden shift in color of a linear gradient in center中心线性渐变的颜色突然变化
【发布时间】:2019-06-28 06:04:23
【问题描述】:

我正在尝试使用 xml 实现如下图所示的行为。请注意,颜色在中间突然变化,而不是颜色的逐渐变化。我认为这可以使用渐变来实现,但我不知道如何。有没有办法使用渐变或使用xml的任何其他方式来实现它?

这是我当前的 xml 代码的样子:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="90"
        android:endColor="#E86D52"
        android:startColor="#E11E26" />
    <corners android:radius="10dp" />
    <stroke
        android:width="1.5dp"
        android:color="#231F20" />
</shape>

这是我想要实现但不知道如何实现的示例图片:

【问题讨论】:

    标签: android android-drawable linear-gradients


    【解决方案1】:

    您可以设置角度来设置边框的水平和垂直渐变和描边颜色。以下是渐变xml的示例-

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape android:shape="rectangle">
                <gradient
                    android:centerColor="#c1c1c1"
                    android:endColor="#4f4f4f"
                    android:gradientRadius="400"
                    android:startColor="#c1c1c1"
                    android:angle="180"
                    />
                <corners android:radius="5dp"/>
    
                <stroke android:width="1dp"
                    android:color="@color/yellow"/>
    
    
                <padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
            </shape>
        </item>
    
    </layer-list>
    

    假设您的渐变是“bg_button.xml”。您将使用 as-

     <Button
    
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:background="@drawable/bg_button"
                           android:text="Next"
                            android:textAllCaps="false"
                          android:textSize="@dimen/btn_text" />
    

    【讨论】:

    • 要求不是答案。你的回答会给出一个漂亮的渐变按钮:)
    • @BugsHappen 你能发布你正在使用的Xml
    • @Manishika 我已经更新了我的问题,请检查 xml 代码
    • 我刚试过你的 XML,它没有任何突然的颜色变化。
    • 我知道不是。我想实现这种突然的转变效果。我怎样才能实现它?这是我的问题。
    猜你喜欢
    • 2014-10-26
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    • 1970-01-01
    相关资源
    最近更新 更多