How to use isInEditMode() to see layout with custom View in the editor

前端 未结 4 697
猫巷女王i
猫巷女王i 2020-11-29 20:03

I must edit a software that have a custom view, when I try to edit layout xml, Eclipse says me:

Use View.isInEditMode() in your custom views to skip

4条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 20:31

    Try this it work for me

    public class MyOwnTextView extends TextView {
    
        public MyOwnTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            // TODO Auto-generated constructor stub
            isInEditMode();
        }
    
        public MyOwnTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            // TODO Auto-generated constructor stub
            isInEditMode();
        }
        public MyOwnTextView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            isInEditMode();
        }
    
        public void setTypeface(Typeface tf, int style) {
            if(!this.isInEditMode()){
            Typeface normalTypeface = Typeface.createFromAsset(getContext().getAssets(), "font/Roboto-Light.ttf");
            Typeface boldTypeface = Typeface.createFromAsset(getContext().getAssets(), "font/Roboto-Light.ttf");
    
            if (style == Typeface.BOLD) {
                super.setTypeface(boldTypeface/*, -1*/);
            } else {
                super.setTypeface(normalTypeface/*, -1*/);
            }
            }
    
        }   
    
    }
    

    then at the xml

    
    

提交回复
热议问题