【问题标题】:Android (Xamarin) selectableItemBackground - SetBackgroundResource not working with SetImageResourceAndroid (Xamarin) selectableItemBackground - SetBackgroundResource 不与 SetImageResource 一起使用
【发布时间】:2018-01-10 16:05:41
【问题描述】:

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

但是,当单击这些按钮时,这些按钮没有所需的按钮单击/涟漪效果。环顾四周后,我找到了几个解决方案,将TypedValueSetBackgroundResource() 结合使用,或者将TypedArraySetBackgroundDrawable() 结合使用。如果没有图片资源,这两种方法都可以,但是在添加图片资源后,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;
    }
}

我该如何解决这个问题?任何帮助将不胜感激!

--编辑--

MyImageButton 代码:

class MyImageButton : ImageButton, IOnTouchListener
{
    private Context context;

    public MyImageButton(Context context, IAttributeSet attrs) : base(context, attrs)
    {
        this.context = context;
        this.SetOnTouchListener(this);
    }

    public bool OnTouch(View v, MotionEvent e)
    {
        if (e.Action == MotionEventActions.Down)
        {
            ImageView iv = (ImageView)v;
            iv.SetColorFilter(new Android.Graphics.Color(context.GetColor(Resource.Color._5_grey)));
        }
        else if (e.Action == MotionEventActions.Up)
        {
            ImageView iv = (ImageView)v;
            iv.ClearColorFilter();
        }
        return true;
    }
};

AXML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Dental_IT.Droid.Adapters.MyImageButton
        android:id="@+id/mainMenu_ImgBtn"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:background="@null" />
</LinearLayout>

【问题讨论】:

    标签: android xamarin imagebutton


    【解决方案1】:

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

    点击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();
    };
    

    效果:

    【讨论】:

    • 动画确实有效,但点击委托事件现在无效
    • @Ryalis,当我可以使用我的电脑时,我会更新我的答案。
    • 当然,没问题。另外,您介意解释一下代码是如何工作的吗?
    • 我已经用不起作用的点击代表更新了我的帖子
    • @Ryalis,我使用你的代码,它在我身边运行良好,你可以调试你的代码找出原因。如果可能的话,你可以分享一个基本的演示来重现这个问题,如果我有一个演示,解决这个问题会容易得多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多