2016-09-30 53 views

回答

3
Please create a drawable file and put the below code in it. 

    <?xml version="1.0" encoding="utf-8"?> 
     <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
      <item> 
       <shape android:shape="rectangle" > 
        <size android:height="20dp" /> 
        <solid android:color="#F66D08" /> 
       </shape> 
      </item> 
      <item android:top="50dp"> 
       <shape android:shape="rectangle" > 
        <gradient android:endColor="#AD1B1D" 
         android:startColor="#E2360A" 
         android:angle="270" /> 
       </shape> 
      </item> 
     </layer-list> 
1

enter image description here將這些代碼在你的activity_main:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:gravity="center" 
     android:background="#F66D08" 
     android:layout_width="match_parent" 
     android:layout_height="20dp"> 

    </LinearLayout> 

    <LinearLayout 
     android:background="@drawable/introslidergradiant" 
     android:layout_width="match_parent" 
     android:layout_height="100dp"></LinearLayout> 
</LinearLayout> 

,創造drawble資源與introslidergradiant.xml文件的文件名:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape> 
      <gradient 
       android:angle="90" 
       android:startColor="#A31720" 
       android:endColor="#E1350B" 
       android:type="linear" /> 
     </shape> 
    </item> 
</selector> 
0
<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape"> 
    <stroke android:width="2dp" android:color="#ff207d94" /> 
    <padding android:left="2dp" 
     android:top="2dp" 
     android:right="2dp" 
     android:bottom="2dp" /> 
    <corners android:radius="5dp" /> 
    <solid android:color="#ffffffff" /> 
</shape> 

你可以在裏面創建一個新的XML文件可繪製的文件夾,並添加上面的代碼,然後將其另存爲rectangle.xml。

要在佈局中使用它,可以將android:background屬性設置爲新的可繪製形狀。我們定義的形狀沒有任何尺寸,因此將採用佈局中定義的視圖的尺寸。

所以把他們放在一起:

<View 
    android:id="@+id/myRectangleView" 
    android:layout_width="200dp" 
    android:layout_height="50dp" 
    android:background="@drawable/rectangle"/> 

最後;您可以將此矩形設置爲任何視圖的背景,但對於ImageView,您可以使用android:src。這意味着你可以使用矩形作爲ListViews,TextViews等的背景。

0

你必須做出一個XML文件中的繪製

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > 
<gradient 
android:type="linear" 
android:startColor="#FFFF820D" 
android:endColor="#FFA32815" 
android:angle="90"/> 
</shape> 

使用下面的代碼,或者你可以生成自己梯度here剛剛設置的顏色和選擇Android選項,它會爲您生成梯度。可用於

5

層列表來解決這個

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

    <item> 
     <shape android:shape="rectangle"> 
      <size 
       android:width="40dp" 
       android:height="40dp" /> 
      <solid android:color="#F86F05" /> 
     </shape> 
    </item> 

    <item android:top="10dp"> 
     <shape android:shape="rectangle"> 
      <size 
       android:width="30dp" 
       android:height="30dp" /> 
      <solid android:color="#B31F19" /> 
     </shape> 
    </item> 

</layer-list> 

而相同的屏幕截圖。

enter image description here

如果你想漸變而不是純色,改變固體梯度設置startColor,ENDCOLOR和角度。

+2

看起來像完美。爲好方法添加'漸變' –