2017-06-29 54 views
0

我使用隨機顏色在color.xml文件由整數數組我cardview背景漸變色的整數數組的Android

color.xml:

<item name="Gold" type="color">#FFFFD700</item> 
<item name="Orange" type="color">#DSADSA</item> 
<item name="LightSalmon" type="color">#EWQ5645W</item> 

<integer-array name="androidcolors"> 
    <item>@color/Gold</item> 
    <item>@color/Orange</item> 
    <item>@color/LightSalmon</item> 
</integer-array> 

,但我想漸變色爲我的背景和我想這:

<integer-array name="androidcolors"> 
    <item><gradient android:endColor="@color/blue" android:startColor="@color/darkblue" /></item> 
</integer-array> 

它不工作,我搜查任何地方,我認爲沒有身體用這個:|你能幫助我如何在整數數組或color.xml文件中使用漸變嗎?

回答

0

嘗試這樣, 1.創建一個可繪製的顏色,例如:

card_drawable_bg.xml 

    <?xml version="1.0" encoding="UTF-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape> 
      <gradient 
android:startColor="#ff9a6922" 
android:centerColor="#ff9a4025" 
android:endColor="#ff9a0e32" 
android:angle="45"/> 
     </shape> 
    </item> 
</selector> 
  • TypedArray是你在找什麼。我有樣品使用它。如果你有興趣,請看看下面的代碼:
  • 首先,整數數組中RES /價值/ arrays.xml:

    <integer-array name="frag_home_ids"> 
        <item>@drawable/card_drawable_bg</item> 
    
    </integer-array> 
    

    第二,獲得資源的整數值編程:

    TypedArray tArray = getResources().obtainTypedArray(
          R.array.frag_home_ids); 
    int count = tArray.length(); 
    int[] ids = new int[count]; 
    for (int i = 0; i < ids.length; i++) { 
        ids[i] = tArray.getResourceId(i, 0); 
    } 
    //Recycles the TypedArray, to be re-used by a later caller. 
    //After calling this function you must not ever touch the typed array again. 
    tArray.recycle(); 
    

    第三,這樣調用的整數值:

    holder.cardView.setCardBackgroundColor(IDS [位置]);

    你可以通過這種方式獲得字符串,顏色,整數,佈局,菜單......的整數值,希望它能幫助你。

    +0

    thx但我想要一個代碼爲許多漸變顏色 –

    +0

    創建另一個可繪製和添加整數數組內 –

    +0

    閱讀此鏈接瞭解更多關於漸變顏色http:// www。 singhajit.com/gradient-color-in-android/ –

    0

    創建一個xml文件並將其放置在Drawable文件夾中。使用下面的代碼

    <?xml version="1.0" encoding="utf-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
    <gradient 
        android:startColor="#8300bf" 
        android:centerColor="#768087" 
        android:endColor="#FF9000" 
        android:angle="-90" 
        android:type="linear"/> 
    

    指定startColor和ENDCOLOR。在您的LinearLayout中,將背景設置爲setBackground="@drawable/Your Gradient Filename"

    +0

    我想要很多漸變顏色 –

    +0

    使用xml,您最多可以使用3種顏色(請查看編輯後的答案)。如果你想使用更多,你必須使用'android.graphics.drawable.GradientDrawable'。 – Abhi

    +0

    你想讓顏色在固定的時間段後保持變化嗎? – Abhi