Eclipse 可视化编辑器中的自定义 Android 视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10743030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom Android Views in Eclipse Visual Editor
提问by fedepaol
In my applications, I often rely on custom build views, such as in the following example.
在我的应用程序中,我经常依赖自定义构建视图,例如在以下示例中。
<?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"
/>
The problem is, I don't know how if it will be displayed correctly until I run it on a real device or on the emulator. Moreover, if I found something wrong I would have to perform changes on it and deploy the app again to see if the changes worked as you expected.
问题是,在我在真实设备或模拟器上运行它之前,我不知道它是否会正确显示。此外,如果我发现有问题,我将不得不对其进行更改并再次部署应用程序,以查看更改是否按您的预期工作。
This can be a long and boring process, especially if the application requires some interaction to get to the activity you want to check.
这可能是一个漫长而乏味的过程,尤其是当应用程序需要一些交互才能访问您要检查的活动时。
Using the visual editor doesn't work as it cannot load the custom view.
使用可视化编辑器不起作用,因为它无法加载自定义视图。
Is there another way to check how views are displayed without running across the whole application?
是否有另一种方法可以在不运行整个应用程序的情况下检查视图的显示方式?
回答by Blundell
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()
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!
这允许您从 ADT 插件 XML 查看器隐藏代码,并希望向您显示布局!
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.
View.isInEditMode()
指示此视图当前是否处于编辑模式。视图在开发人员工具中显示时通常处于编辑模式。例如,如果此 View 是由可视化用户界面构建器绘制的,则此方法应返回 true。如果子类的正常行为可能会干扰宿主环境,则子类应检查此方法的返回值以提供不同的行为。例如:类在其构造函数中生成一个线程,绘制代码依赖于特定于设备的功能等。通常在自定义小部件的绘制代码中检查该方法。
回答by cstrutton
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.
您可以创建一个骨架活动,它只加载您想要查看的视图,并用足够的数据填充它以使其显示。
回答by Luis Sieira
I'm using Android Studio so I'm not sure this answer will apply to your case.
我正在使用 Android Studio,所以我不确定这个答案是否适用于您的情况。
I think you could override onDraw method in the custom view, like this exemple keeping the aspect ratio of an inner image:
我认为您可以在自定义视图中覆盖 onDraw 方法,例如保持内部图像的纵横比:
@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...)
它也适用于任何重绘视图的事件(onSizeChanged、onLayout 等...)