2011-09-12 45 views

回答

70

創建gradient.xml/res/drawable

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <gradient 
     android:startColor="#FFFFFF" 
     android:endColor="#00000000" 
     android:angle="45"/>  
</shape> 

,並在您main.xml佈局文件中/res/layout

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/gradient" 
    > 
</LinearLayout> 

您可以通過更換android:angle值指定角度和開始/結束顏色通過替代android:startColorandroid:endColor

+0

我們可以在運行時更改xml梯度值 –

8

您可以使用這樣的事情:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle"> 
    <gradient android:startColor="#A1A1A1" 
       android:centerColor="#BDBDBD" 
       android:endColor="#A4A4A4" 
       android:angle="-90" /> 
</shape> 

建立一個梯度(你選擇你喜歡的顏色)。把它放在drawable中,並且你有自己的形狀作爲背景:android:background="@drawable/the_name_of_your_xml"

4

這就是我設置漸變樣式的方法。希望這可以幫助。但我用它來用於textview。也許你必須做一些改變以適應你的佈局背景。

  Shader textShader = new LinearGradient(0, 0, 0, 20, new int[] { 
      Color.WHITE, getResources().getColor(//some color), 
      getResources().getColor(//some color), Color.WHITE }, 
      new float[] { 0.25f,0.50f,0.75f, 1 }, TileMode.CLAMP); 
      textview.getPaint().setShader(textShader); 
+2

儘管他在問題中提到他希望有一個xml解決方案,而不是運行時問題 – DonGru

+0

,如果我想在運行時更改xml梯度值 –