2017-03-01 17 views
4

我一直在嘗試在XML中製作多色背景,但只有3個選項可用,包括開始,中心,結束和指定的角度。我們不能做的背景是這樣..下面multi color at different angle 我們可以在xml中爲android背景製作多色漸變嗎?

multi color at different angle

我們可以作出這樣的背景中Android的??

+0

看到http://stackoverflow.com/questions/14020530/using-a-gradientdrawable-with-more-than-three-colors-set和http://stackoverflow.com/questions/4381033/multi -gradient-shapes –

+0

你能告訴我怎麼樣? –

+0

這些只是我相信的兩種顏色。這些漸變是可以做到的。 – CoderP

回答

7

根據developers.android 你可以...這是他們用

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" > 

<gradient 
    android:angle="45" 
    android:endColor="#87CEEB" 
    android:centerColor="#768087" 
    android:startColor="#000" 
    android:type="linear" /> 

</shape> 

here's教程

希望這有助於

+1

我需要多種顏色不僅3 ..但無論如何,我我有沒有做過感謝幫助。 –

+6

Tapan Kumar Patro請分享您所做的工作來實現它 –

0

你可以用層列表做它的代碼的徑向漸變,那麼你爲不同的物品設置不同的不透明度

3

你不能實現+3漸變顏色在xml文件中。但是,您可以使用GradientDrawable類將其添加到您的java/kotlin代碼中。這是Java版本,用您的顏色ID替換顏色數組。

GradientDrawable gradientDrawable = new GradientDrawable(
       Orientation.TOP_BOTTOM, 
       new int[]{ContextCompat.getColor(this, R.color.color1), 
         ContextCompat.getColor(this, R.color.color2), 
         ContextCompat.getColor(this, R.color.color3), 
         ContextCompat.getColor(this, R.color.color4)}); 

     findViewById(R.id.background).setBackground(gradientDrawable);