【问题标题】:Custom Android Views in Eclipse Visual EditorEclipse 可视化编辑器中的自定义 Android 视图
【发布时间】:2012-05-31 08:44:21
【问题描述】:

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

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

/>

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

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

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

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

【问题讨论】:

  • 感谢@Blundell 修改我的问题

标签: android eclipse android-layout adt visual-editor


【解决方案1】:

您可以在自定义视图中执行此操作:

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()

指示此视图当前是否处于编辑模式。一个视图是 通常在开发人员工具中显示时处于编辑模式。为了 例如,如果这个视图是由可视用户界面绘制的 builder,这个方法应该返回true。子类应该检查 此方法的返回值以提供不同的行为,如果他们 正常行为可能会干扰宿主环境。为了 实例:该类在其构造函数中生成一个线程,即绘图 代码依赖于设备特定的功能等。这种方法通常是 签入自定义小部件的绘制代码。

【讨论】:

    【解决方案2】:

    您可以创建一个框架 Activity,它只加载您想要查看的视图并使用足够的数据填充它以使其显示。

    【讨论】:

      【解决方案3】:

      我正在使用 Android Studio,所以我不确定这个答案是否适用于您的情况。

      我认为您可以在自定义视图中覆盖 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);
          }
      }
      

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

      它也适用于任何重绘视图的事件(onSizeChanged、onLayout 等...)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-05
        • 2015-09-14
        • 1970-01-01
        • 1970-01-01
        • 2018-01-02
        • 1970-01-01
        • 2010-09-24
        • 2020-09-09
        相关资源
        最近更新 更多