2013-08-23 45 views
0

我想創建一個按鈕,其包括梯度以及圖像,如下所示: enter image description here添加梯度機器人按鈕與抽拉

圖像的背景應該梯度。

+0

[你可以從這裏這個答案] [1]剛剛做的步驟,它會幫助你... :) [1]:http://stackoverflow.com/questions/10449311/android-selector-with-background-image-and-gradient – arraystack

+0

謝謝,它的工作原理。 – user2523388

回答

0

關注該https://stackoverflow.com/a/10066352/942224

您可以動態創建漸變繪製..使用下面類

import android.graphics.drawable.GradientDrawable; 

public class SomeDrawable extends GradientDrawable { 

public SomeDrawable(int pStartColor, int pCenterColor, int pEndColor, int pStrokeWidth, int pStrokeColor, float cornerRadius) { 
    super(Orientation.BOTTOM_TOP,new int[]{pStartColor,pCenterColor,pEndColor}); 
    setStroke(pStrokeWidth,pStrokeColor); 
    setShape(GradientDrawable.RECTANGLE); 
    setCornerRadius(cornerRadius); 
} 

}

,並使用這個類,如下

SomeDrawable drawable = new SomeDrawable(Color.parseColor("Start Color Code"),Color.parseColor("Center Color Code"),Color.parseColor("End Color Code"),1,Color.BLACK,00); 
yourLayout.setBackgroundDrawable(drawable); 

,你可以也爲該視圖設置圖像資源。

+0

即可,但也需要放置圖標。 – user2523388

2
<ImageView 
      android:id="@+id/ivdpfirst" 
      android:layout_width="100dp" 
      android:layout_height="100dp" 
      android:background="@drawable/sidebutton" 
      android:src="@drawable/ic_me1" /> 

sidebutton.xml在繪製文件夾:

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

<item><shape> 
     <gradient android:angle="270" android:endColor="#000000" android:startColor="#396AA1" /> 

     <stroke android:width="1dp" android:color="#000000" /> 

     <corners android:radius="5dp" /> 

     <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" /> 
    </shape></item> 

</selector>