首页 > 解决方案 > CustomButton android,覆盖 OnCreateDrawableState 导致应用程序崩溃

问题描述

我创建了一个继承自RelativeLayout 的自定义按钮,我向该按钮添加了一个自定义状态“state_full_version”。

当我 ovveride OnCreateDrawableState 应用程序崩溃并显示以下消息:

在此处输入图像描述

在下面找到我的 CustomButton 代码:

  <declare-styleable name="CustomButton">
<attr name="android:text" />
<attr name="android:textColor" />
<attr name="android:textSize" />
<attr name="android:drawable" />
<attr name="android:padding" />
<attr name="onlyInFullVersion" format="boolean" />
<attr name="state_full_version" format="boolean" />

和 c# 代码

public class CustomButton : RelativeLayout
{
    public CustomButton(Context context) : base(context, null)
    {
        this.Initialize(context, null);
    }

    public CustomButton(Context context, IAttributeSet attrs) : base(context, attrs)
    {
        this.Initialize(context, attrs);
    }

    public CustomButton(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
    {
        this.Initialize(context, attrs);
    }
    public string Text
    {
        get => this._textView.Text;
        set => this._textView.Text = value;
    }

    private bool _onlyInFullVersion;
    private TextView _textView;
    private void Initialize(Context context, IAttributeSet attrs)
    {
        string text = string.Empty;
        Color textColor = Color.White;
        Drawable image = null;
        int textSize = 0;
        int padding = 0;
        if (attrs != null)
        {
            TypedArray a = this.Context.ObtainStyledAttributes(attrs, Resource.Styleable.CustomButton);
            text = a.GetString(Resource.Styleable.CustomButton_android_text);
            textColor = a.GetColor(Resource.Styleable.CustomButton_android_textColor, 0);
            textSize = a.GetDimensionPixelSize(Resource.Styleable.CustomButton_android_textSize, 0);
            image = a.GetDrawable(Resource.Styleable.CustomButton_android_drawable);
            padding = a.GetDimensionPixelSize(Resource.Styleable.CustomButton_android_padding, 0);
            this._onlyInFullVersion = a.GetBoolean(Resource.Styleable.CustomButton_onlyInFullVersion, false);
            a.Recycle();
        }

        this.Clickable = true;
        LinearLayout linearLayout = new LinearLayout(context, null);
        linearLayout.Orientation = Orientation.Horizontal;
        ImageView imageView = new ImageView(context);
        if (image != null)
            imageView.SetImageDrawable(image);
        imageView.SetAdjustViewBounds(true);
        imageView.SetPadding(padding, padding, padding, padding);
        imageView.SetScaleType(ImageView.ScaleType.CenterInside);
        LayoutParams layoutParamsImage = new LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.MatchParent);
        layoutParamsImage.AddRule(LayoutRules.CenterVertical);
        linearLayout.AddView(imageView, layoutParamsImage);

        this._textView = new TextView(context, null);
        this._textView.Typeface = UiHelper.FontRegular;
        this._textView.Text = text;
        this._textView.SetTextSize(ComplexUnitType.Px, textSize);
        this._textView.SetTextColor(textColor);
        this._textView.Gravity = GravityFlags.CenterVertical;
        this._textView.SetPadding(padding, padding, padding, padding);

        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.MatchParent);
        layoutParams.AddRule(LayoutRules.CenterVertical);
        linearLayout.AddView(this._textView, layoutParams);

        LayoutParams layoutParamsLinearLayout = new LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.MatchParent);
        layoutParamsLinearLayout.AddRule(LayoutRules.CenterVertical);
        layoutParamsLinearLayout.AddRule(LayoutRules.CenterHorizontal);
        this.AddView(linearLayout, layoutParamsLinearLayout);

        if (this._onlyInFullVersion && !InAppManager.IsFullVersion)
        {
            this.Background.SetState(new[] { Resource.Attribute.state_full_version });
            this.RefreshDrawableState();
        }
    }

    protected override int[] OnCreateDrawableState(int extraSpace)
    {
        int[] drawableState = base.OnCreateDrawableState(extraSpace + 1);

        if (this._onlyInFullVersion && InAppManager.IsFullVersion)
            MergeDrawableStates(drawableState, new[] { Resource.Attribute.state_full_version });
        return (drawableState);
    }
}

如果我评论 OnCreateDrawableState 的覆盖,则代码有效。

我做错了什么?

标签: androidxamarin.androidcontrols

解决方案


推荐阅读