我想创建一个包含渐变和图像的按钮,如图所示:
图像的背景应该是渐变的。
<ImageView
android:id="@+id/ivdpfirst"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/sidebutton"
android:src="@drawable/ic_me1" />
sidebutton.xml in drawable folder:
<?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>
follow this https://stackoverflow.com/a/10066352/942224
you can create gradient drawable dynamically.. use below class
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);
}
}
and use this class as below
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);
and you can set image resource also for that view.