将渐变颜色添加到android中的按钮背景

时间:2018-12-09 09:44:29

标签: android

我有一个带代码的渐变背景

<Button android:id="@+id/btn_signup"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Login"
            android:background="@drawable/sel_btn_background"
            android:gravity="center"
            android:textSize="16sp"
            android:layout_marginTop="40dp"
            android:textColor="@android:color/white"/>

sel_btn_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <gradient
            android:startColor="@color/bluegreen"
            android:endColor="@color/aqua_marine"
            android:angle="-270" />
</shape>

我得到结果

enter image description here

如何获得以下结果(左侧是浅色逐渐消失为右侧上的深色)应为角度添加什么值:

enter image description here

3 个答案:

答案 0 :(得分:0)

应为360,请尝试以下操作:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#36d1d1"
        android:centerColor="#811010"
        android:endColor="#233308"
        android:angle="360" />
</shape>

相应地替换您的颜色值。我在所有3种颜色之间使用了完全不同的颜色区分。

答案 1 :(得分:0)

尝试一下...

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#278b79"
        android:centerColor="#32b39b"
        android:endColor="#38c7ad"
        android:angle="180" />
</shape>

答案 2 :(得分:0)

  

使用180角而不是-270角

android:angle="360"