Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android(Xamarin)selectableItemBackground-SetBackgroundResource无法与SetImageResource一起使用_Android_Xamarin_Imagebutton - Fatal编程技术网

Android(Xamarin)selectableItemBackground-SetBackgroundResource无法与SetImageResource一起使用

Android(Xamarin)selectableItemBackground-SetBackgroundResource无法与SetImageResource一起使用,android,xamarin,imagebutton,Android,Xamarin,Imagebutton,我使用一个自定义网格适配器创建图像按钮并在活动上显示它们,使用方法SetImageResource()设置按钮的src图像 但是,单击时,这些按钮没有所需的按钮单击/涟漪效果。环顾四周后,我找到了两种解决方案,使用TypedValue和SetBackgroundResource(),或者使用TypedArray和SetBackgroundDrawable()。如果没有图像资源,这两种方法都可以工作,但是添加图像资源后,selectableItemBackground效果消失 代码如下: Imag

我使用一个自定义网格适配器创建图像按钮并在活动上显示它们,使用方法
SetImageResource()
设置按钮的src图像

但是,单击时,这些按钮没有所需的按钮单击/涟漪效果。环顾四周后,我找到了两种解决方案,使用
TypedValue
SetBackgroundResource()
,或者使用
TypedArray
SetBackgroundDrawable()
。如果没有图像资源,这两种方法都可以工作,但是添加图像资源后,
selectableItemBackground
效果消失

代码如下:

ImageButton button;

if (convertView == null)
{
    LayoutInflater inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);

    //  Grid buttons
    convertView = inflater.Inflate(Resource.Layout.sublayout_Menu_Button, null);

    button = convertView.FindViewById<ImageButton>(Resource.Id.mainMenu_ImgBtn);

    button.SetMinimumHeight(Main_Menu.GRID_HEIGHT / numRows);

    TypedValue tv = new TypedValue();
    context.Theme.ResolveAttribute(Resource.Attribute.selectableItemBackground, tv, true);
    button.SetBackgroundResource(tv.ResourceId);

    //  This code is another method that works similarly to the above                
    //int[] attrs = new int[] { Android.Resource.Attribute.SelectableItemBackground };
    //TypedArray ta = context.ObtainStyledAttributes(attrs);
    //Drawable drawableFromTheme = ta.GetDrawable(0);
    //ta.Recycle();
    //button.SetBackgroundDrawable(drawableFromTheme);

    button.SetImageResource(buttonImages[position]);

    switch (position)
    {
        case 0:
            button.Click += delegate
            {
                Intent intent = new Intent(context, typeof(Select_Hospital));
                context.StartActivity(intent);
            };
            break;

        case 1:
            button.Click += delegate
            {
                Intent intent = new Intent(context, typeof(My_Appointments));
                context.StartActivity(intent);
            };
            break;

        case 2:
            button.Click += delegate
            {
                Intent intent = new Intent(context, typeof(Treatment_Information));
                context.StartActivity(intent);
            };
            break;

        case 3:
            button.Click += delegate
            {
                Intent intent = new Intent(context, typeof(Search));
                context.StartActivity(intent);
            };
            break;
    }
}
AXML:


单击时,这些按钮没有所需的按钮单击/涟漪效果

单击
ImageButton
时,确实会产生按钮点击/涟漪效应,但它被图像覆盖

您可以自定义一个
ImageButton
来处理触摸事件,当收到点击事件时,您可以在图像上添加一个
ColorFilter
,以实现点击/涟漪效果

这是我的密码:

public class MyImageButton : ImageButton
{
    public float[] BT_SELECTED_DARK = new float[] { 1, 0, 0, 0, -50, 0, 1,
            0, 0, -50, 0, 0, 1, 0, -50, 0, 0, 0, 1, 0 };

    public MyImageButton(Context context, IAttributeSet attrs):base(context,attrs)
    {
    }

    public override bool OnTouchEvent(MotionEvent e)
    {
        if (e.Action == MotionEventActions.Down)
        {
            this.SetColorFilter(new ColorMatrixColorFilter(BT_SELECTED_DARK));
        }
        else if (e.Action == MotionEventActions.Up)
        {
            this.ClearColorFilter();
        }
        return base.OnTouchEvent(e);
    }
}
添加图像资源时:

button.SetImageResource(Resource.Drawable.download);
button.Click += (sender, e) =>
{
    Toast.MakeText(this,"Hi, I am York!",ToastLength.Short).Show();
};
效果:


动画确实有效,但单击代理事件不起作用now@Ryalis,当我可以使用电脑时,我会更新我的答案。当然,没问题。另外,你介意解释一下代码是如何工作的吗?我已经用点击代理更新了我的帖子,但没有work@Ryalis,我使用您的代码,它在我身边运行良好,您可以调试代码以找到原因。如果可能的话,你可以分享一个基本的演示来重现这个问题,如果我有一个演示的话,解决这个问题会容易得多。
public class MyImageButton : ImageButton
{
    public float[] BT_SELECTED_DARK = new float[] { 1, 0, 0, 0, -50, 0, 1,
            0, 0, -50, 0, 0, 1, 0, -50, 0, 0, 0, 1, 0 };

    public MyImageButton(Context context, IAttributeSet attrs):base(context,attrs)
    {
    }

    public override bool OnTouchEvent(MotionEvent e)
    {
        if (e.Action == MotionEventActions.Down)
        {
            this.SetColorFilter(new ColorMatrixColorFilter(BT_SELECTED_DARK));
        }
        else if (e.Action == MotionEventActions.Up)
        {
            this.ClearColorFilter();
        }
        return base.OnTouchEvent(e);
    }
}
button.SetImageResource(Resource.Drawable.download);
button.Click += (sender, e) =>
{
    Toast.MakeText(this,"Hi, I am York!",ToastLength.Short).Show();
};