[请注意,我使用Xamarin.Droid
,这是一个使用Mono.NET C#的跨平台框架,该代码确实接近Android的Java,并且我接受Java响应,因为它易于翻译到C#]
我将Button子类化,然后应用带有颜色和shape
的{{1}}。一切正常,除了按下按钮时我失去了涟漪效果。
我尝试添加leftDrawable
,但是使用代码,因为该子类没有任何XML,并且波纹效果仍然没有显示。
这是我的按钮子类
?attr/selectableItemBackground
答案 0 :(得分:1)
为波纹可绘制对象创建XML文件,如下所示:
my_button_background_v21.xml
<?xml version="1.0" encoding="UTF-8" ?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="@color/black"
tools:targetApi="lollipop">
<item>
<shape android:shape="rectangle">
<corners android:radius="100dp" />
<solid android:color="@color/LTGreen" />
</shape>
</item>
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<corners android:radius="100dp" />
<solid android:color="@color/black" />
</shape>
</item>
然后在自定义按钮的onDraw方法中添加如下所示的涟漪效果:
protected override void OnDraw(Canvas canvas)
{
base.OnDraw(canvas);
Drawable img = ContextCompat.GetDrawable(context, Resource.Drawable.chevron_right);
img.SetBounds(0, 0, 70, 70);
SetCompoundDrawables(img, null, null, null);
TextSize = 16;
if (Build.VERSION.SdkInt < BuildVersionCodes.Lollipop)
{
SetBackgroundResource(Resource.Layout.my_button_background);
}
else
{
SetBackgroundResource(Resource.Layout.my_button_background_v21);
}
}
答案 1 :(得分:0)
使用Java可以这样:
自定义 RippleButton
`let getQuery = CalendarEvent.getRepository()
.createQueryBuilder('event')
.leftJoinAndSelect('event.slots', 'slots')
.where('event.belongsToOrganization = :orgId', { orgId: orgId })
.select('COUNT(event.id)')
.addSelect('slots.startTime')
.groupBy('slots.startTime')
.orderBy('slots.startTime', 'ASC')
.getMany();`
自定义一个 RippleDrawable :
public class RippleButton extends Button {
private RippleDrawable rippleDrawable;
private Paint paint;
public RippleButton(Context context) {
this(context,null);
}
public RippleButton(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public RippleButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
paint = new Paint();
rippleDrawable = new RippleDrawable();
//Set refresh interface, View has been implemented ---> source button inheritance child drawable.callback
rippleDrawable.setCallback(this);
//rippleDrawable
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
//Set the refresh area ---> source code
rippleDrawable.setBounds(0,0,getWidth(),getHeight());
}
@Override
protected boolean verifyDrawable(Drawable who) {
return who == rippleDrawable || super.verifyDrawable(who);
}
@Override
protected void onDraw(Canvas canvas) {
rippleDrawable.draw(canvas);
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
rippleDrawable.onTouch(event);
return true;
}
}
如果有帮助,请标记答案以帮助他人。
答案 2 :(得分:0)
由于@ G.Hakim和this thread,我能够重现我的背景,并仅使用XML添加波纹效果。
my_button_background.xml
<?xml version="1.0" encoding="UTF-8" ?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="@color/black"
tools:targetApi="lollipop">
<item>
<shape android:shape="rectangle">
<corners android:radius="100dp" />
<solid android:color="@color/LTGreen" />
</shape>
</item>
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<corners android:radius="100dp" />
<solid android:color="@color/black" />
</shape>
</item>
</ripple>
MyButton.cs
protected override void OnDraw(Canvas canvas)
{
base.OnDraw(canvas);
Drawable img = ContextCompat.GetDrawable(context, Resource.Drawable.chevron_right);
img.SetBounds(0, 0, 70, 70);
SetCompoundDrawables(img, null, null, null);
TextSize = 16;
if (Build.VERSION.SdkInt < BuildVersionCodes.Lollipop)
{
SetBackgroundResource(Resource.Layout.my_button_background);
}
else
{
SetBackgroundResource(Resource.Layout.my_button_background_v21);
}
}