English 中文(简体)
自定义和机器人在日光视觉编辑器中的观点
原标题:Custom Android Views in Eclipse Visual Editor

在我的申请中,我经常依靠自定义的建立观点,例如以下的例子。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:background="@color/light_grey"
android:layout_height="match_parent" 
android:layout_width="fill_parent" >

<TextView 
 style="@style/CardTitle" 
 android:id="@+id/card_title"
 android:layout_height="wrap_content" 
 android:layout_width="fill_parent"      
 />  

<com.whiterabbit.cards.ui.AspectRatioImageView
    android:id="@+id/card_picture"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:layout_marginLeft="30dip"
    android:layout_marginRight="30dip"       
    android:src="@drawable/boss"
    />



<ListView 
    android:id="@+id/card_properties" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"

/>

问题是,我不知道它会如何被正确显示,直到我用一个真正的设备或模拟器运行它。 此外,如果我发现有问题,我不得不对它进行修改并再次使用应用程序来查看这些修改是否如您所期望的那样有效。

这可能是一个漫长而无聊的过程,特别是如果应用程序需要某种互动才能进入您想要检查的活动。

使用视觉编辑器无效, 因为它无法装入自定义视图 。

是否有其他方法可以检查视图如何显示而不在整个应用程序中运行?

最佳回答

您可以在自定义视图中做到这一点 :

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(

这允许您隐藏 ADT 插件 XML 查看器的代码, 希望您能够显示一个布局!

View. is InditMode ()

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.

问题回答

您可以创建一个包含您想要看到的视图的骨干活动, 并用足够数据来显示它 。

我用Android工作室 所以我不确定这个答案会适用于你的案子

我想你可以在自定义视图中 取代在拖网上的方法, 就像这个例子, 保持一个内部图像的侧边比 :

@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);
    }
}

此方法在模拟器和设计师中运行。

任何重画视图的事件都同样有效(以Size Changed、Layout等为例)。





相关问题
In Eclipse, why doesn t "Show In" appear for non-Java files?

If I have a *java file open, I can right click on the source, scroll down to "Show In (Alt-Shift-W)", and select Navigator. If I have an *xml, *jsp, or pretty much anything besides a Java source ...

Eclipse: Hover broken in debug perspective

Since upgrading Eclipse (Galileo build 20090920-1017), hover in debug no longer displays a variable s value. Instead, hover behaves as if I were in normal Java perspective: alt text http://...

Eclipse smart quotes - like in Textmate

Happy Friday — Does anyone know if eclipse has the notion of smart quotes like Textmate. The way it works is to select some words and quote them by simply hitting the " key? I m a newbie here so be ...

Indentation guide for the eclipse editor

Is there a setting or plugin for eclipse that can show indentation guides in the editor? Something like the Codekana plugin for visual studio (not so fancy is also OK). Important that it works with ...

Adding jar from Eclipse plugin runtime tab

I want to add .jar files for plugin from the Runtime tab of manifest file. When I use the Add... button, I can see only sub-directories of the plugin project. So if I want to add same .jar file to ...

How to copy multiple projects into single folder in eclipse

I have two projects, Project1 and Project2. Now i want to copy this projects into eclipse workspace but i want these projects to be in a single folder like below. Eclipse Workspace -> Project -> ...

Java: Finding out what is using all the memory

I have a java application that runs out of memory, but I have no idea which code is allocating the memory. Is there an application with which I can check this? I use Eclipse.

热门标签