Custom Android Views in Eclipse Visual Editor

回眸只為那壹抹淺笑 提交于 2019-11-28 08:33:50

You can do this in your Custom View:

if(!isInEditMode()){
   // Your custom code that is not letting the Visual Editor draw properly
   // i.e. thread spawning or other things in the constructor
}

http://developer.android.com/reference/android/view/View.html#isInEditMode()

This allows you to hide code from the ADT Plugin XML Viewer and hopefully display you a layout!

View.isInEditMode()

Indicates whether this View is currently in edit mode. A View is usually in edit mode when displayed within a developer tool. For instance, if this View is being drawn by a visual user interface builder, this method should return true. Subclasses should check the return value of this method to provide different behaviors if their normal behavior might interfere with the host environment. For instance: the class spawns a thread in its constructor, the drawing code relies on device-specific features, etc. This method is usually checked in the drawing code of custom widgets.

You could create a skeleton activity that loads just the view you want to see and populate it with enough data to make it display.

I'm using Android Studio so I'm not sure this answer will apply to your case.

I think you could override onDraw method in the custom view, like this exemple keeping the aspect ratio of an inner image:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    // TODO: consider storing these as member variables to reduce
    // allocations per draw cycle.
    int paddingLeft = getPaddingLeft();
    int paddingTop = getPaddingTop();
    int paddingRight = getPaddingRight();
    int paddingBottom = getPaddingBottom();

    int w = getWidth() - paddingLeft - paddingRight;
    int h = getHeight() - paddingTop - paddingBottom;

    w = w<h ? w : h;
    h = w;

    // Draw the example drawable on top of the text.
    if (dieDrawable != null) {
        dieDrawable.setBounds(paddingLeft, paddingTop,
        paddingLeft + w, paddingTop + h);
        dieDrawable.draw(canvas);
    }
}

This method runs both in the emulator and the designer.

It runs as well for any event that redraws the view (onSizeChanged, onLayout, etc...)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!