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

前端 未结 4 656
猫巷女王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

    <com.xxxxx.appname.MyOwnTextView
    
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
    
            />
    
    0 讨论(0)
  • 2020-11-29 20:35
    public class CustomTextView extends TextView {
    
    public CustomTextView(Context context) {
        super(context);
    }
    
    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    
        if (!isInEditMode()) {
            createTypeface(context, attrs); //whatever added functionality you are trying to add to Widget, call that inside this condition. 
        }
    
    }
    
    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    
    //Typeface I wan to set to my Custom TextView It can be any other functionality of your choice
    
    private void createTypeface(Context context, AttributeSet attrs) {
    
        TypedArray styledAttrs = context.obtainStyledAttributes(attrs,
                R.styleable.CustomTextView);
        String fontName = styledAttrs
                .getString(R.styleable.CustomTextView_Typeface);
        styledAttrs.recycle();
    
        if (fontName != null) {
            Typeface typeface = Typeface.createFromAsset(context.getAssets(),
                    "fonts/" + fontName);
    
            setTypeface(typeface);
            styledAttrs.recycle();
        }
    }
    
    }
    
    0 讨论(0)
  • 2020-11-29 20:42

    The following classes could not be instantiated: - com.google.android.gms.ads.AdView (Open Class, Show Exception, Clear Cache)

    Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE. If this is an unexpected error you can also try to build the project, then manually refresh the layout.

    Exception Details java.lang.NoClassDefFoundError: 
        com/google/android/gms/ads/R$styleable   at 
        com.google.android.gms.internal.ads.zzuq.<init>(com.google.android.gms:play-services-ads-lite@@18.3.0:45)   at com.google.android.gms.internal.ads.zzxl.<init>(com.google.android.gms:play-services-ads-lite@@18.3.0:30)   at com.google.android.gms.internal.ads.zzxl.<init>(com.google.android.gms:play-services-ads-lite@@18.3.0:52)   at com.google.android.gms.internal.ads.zzxl.
    

    <init>(com.google.android.gms:play-services-ads-lite@@18.3.0:17)   at com.google.android.gms.ads.BaseAdView.<init>(com.google.android.gms:play-services-ads-lite@@18.3.0:5)   at com.google.android.gms.ads.AdView.<init>(com.google.android.gms:play-services-ads-lite@@18.3.0:4)   at java.lang.reflect.Constructor.newInstance(Constructor.java:423)   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)   at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:863)   at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)   at android.view.LayoutInflater.rInflate(LayoutInflater.java:837)   at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)   at android.view.LayoutInflater.inflate(LayoutInflater.java:515)   at android.view.LayoutInflater.inflate(LayoutInflater.java:394) Copy stack to clipboard

    0 讨论(0)
  • 2020-11-29 20:43

    isInEditMode()should be used inside the Custom View constructor. Try the following code:

         public class GraphView extends View implements Grapher
            {
    
             public GraphView(Context context, AttributeSet attrs) {
                    super(context, attrs);
                    if(!isInEditMode())
                     init(context);
                }
    
                public GraphView(Context context) {
                    super(context);
                   if(!isInEditMode()){
                    touchHandler = new TouchHandler(this);
                    init(context);
                  }
                }
    
    0 讨论(0)
提交回复
热议问题