android custom view attributes not working after switch to gradle

前端 未结 5 2188
小蘑菇
小蘑菇 2021-02-02 00:29

so I recently migrated to gradle now my custom view attributes return null

my project looks like this

--custom_icon_view // library that holds the custom view wi

5条回答
  •  忘掉有多难
    2021-02-02 01:07

    If you are grabbing attributes in your custom view's init method with hard references to that specific namespace then changing to res-auto will break that. Instead, you will need to change those references also to res-auto. Or the preferred method is to just grab the typed interface. For example, in my project this:

    textStyle = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/com.bodybuilding.mobile", "textStyle", 10);
    shadowInner = attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res/com.bodybuilding.mobile", "shadowInner", true);
    shadowOuter = attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res/com.bodybuilding.mobile", "shadowOuter", false);
    allowResize = attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res/com.bodybuilding.mobile", "allowResize", true);
    

    Became either this:

    textStyle = attrs.getAttributeIntValue("http://schemas.android.com/apk/res-auto", "textStyle", 10);
    shadowInner = attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res-auto", "shadowInner", true);
    shadowOuter = attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res-auto", "shadowOuter", false);
    allowResize = attrs.getAttributeBooleanValue("http://schemas.android.com/apk/res-auto", "allowResize", true);
    

    but preferably this:

    TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.BBcomButton);
    textStyle = a.getInt(R.styleable.BBcomButton_textStyle, 10);
    shadowInner = a.getBoolean(R.styleable.BBcomButton_shadowInner, true);
    shadowOuter = a.getBoolean(R.styleable.BBcomButton_shadowOuter, false);
    shadowInnerColor = a.getColor(R.styleable.BBcomButton_shadowInnerColor, 0xff000000);
    shadowOuterColor = a.getColor(R.styleable.BBcomButton_shadowOuterColor, 0xff000000);
    allowResize = a.getBoolean(R.styleable.BBcomButton_allowResize, true);
    

    To make it work

提交回复
热议问题