android - Eclipse 可视化编辑器中的自定义 Android View

标签 android eclipse android-layout adt visual-editor

在我的应用程序中,我经常依赖自定义构建 View ,例如以下示例。

<?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"

/>

问题是,我不知道它是否会正确显示,直到我在真实设备或模拟器上运行它。此外,如果我发现有问题,我将不得不对其进行更改并再次部署该应用程序,以查看更改是否如您预期的那样有效。

这可能是一个漫长而乏味的过程,尤其是当应用程序需要一些交互才能到达您要检查的 Activity 时。

使用可视化编辑器不起作用,因为它无法加载自定义 View 。

是否有另一种方法可以在不运行整个应用程序的情况下检查 View 的显示方式?

最佳答案

您可以在自定义 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()

这允许您从 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.

关于android - Eclipse 可视化编辑器中的自定义 Android View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10743030/

相关文章:

android - 工具命名空间的可用属性列表

android - 将图像顶部与 TextView 顶部对齐

android - 长按菜单按钮关闭 Android 2.3 上的对话框

java - 从解析数据库 Android 中提取 INT

android - 如何使用Graph API获取用户好友的个人资料ID

eclipse - Enterprise Eclipse Provisioning - 或者 - 如何与其他开发人员共享您的标准 Eclipse 设置

android - 在ubuntu中使用eclipse做android项目必须使用Sun Java吗?我已经安装了 OpenJDK

java - Android:如何将输入从 edittext 存储到 .csv

java - 暂停计时器然后继续

java - 可视化 Java 类型层次结构中的类,它们实现了不同的功能?