Resources$NotFoundException in Graphical Layout ADT preview (but app actually Works)

前端 未结 2 553
醉酒成梦
醉酒成梦 2020-12-17 01:02

My problem is that loading an array of strings defined in XML works in the app but will result in an error in the ADT Graphical Layout preview.

Now I can\'t see any

2条回答
  •  旧巷少年郎
    2020-12-17 01:37

    I found a kind of a workaround whereby you have to hijack android's own attributes to get access to resources in the designer.

    The following should provide the idea, but you would have to find a native android property of type int[]

    This custom view XML should render in the graphical layout preview while using resources

    
    
    

    styles.xml - Style specifying some of the resources to use

    
    

    attrs.xml - Custom attribute definition compatible with design-time preview

     
    
        
        
        
        
        
        
        
        
        
        
        
        
        
    
    

    ShowcaseView.java - Using the custom attributes in the custom view

    public ShowcaseView(Context context) {
        this(context, null, R.styleable.CustomTheme_showcaseViewStyle);
    }
    
    public ShowcaseView(Context context, AttributeSet attrs) {
        this(context, attrs, R.styleable.CustomTheme_showcaseViewStyle);
    }
    
    public ShowcaseView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // Get the attributes for the ShowcaseView
        final TypedArray styled = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ShowcaseView, 0, 0);
        showcase = styled.getDrawable(R.styleable.ShowcaseView_android_src);
        titleText = styled.getString(R.styleable.ShowcaseView_android_contentDescription);
        subText = styled.getString(R.styleable.ShowcaseView_android_description);
        buttonText = styled.getString(R.styleable.ShowcaseView_android_text);
        backColor = styled.getInt(R.styleable.ShowcaseView_sv_backgroundColor, Color.argb(128, 80, 80, 80));
        detailTextColor = styled.getColor(R.styleable.ShowcaseView_sv_detailTextColor, Color.WHITE);
        titleTextColor = styled.getColor(R.styleable.ShowcaseView_sv_titleTextColor, Color.parseColor("#49C0EC"));
        styled.recycle();
        // Now make use of the fields / do further initialization ..
    }
    

提交回复
热议问题