Eclipse可视化编辑器中的自定义Android视图
在我的应用程序中,我经常依赖自定义构建视图,如下面的示例中所示Eclipse可视化编辑器中的自定义Android视图,android,eclipse,android-layout,adt,visual-editor,Android,Eclipse,Android Layout,Adt,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="mat
<?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
}
这允许您从ADT插件XML查看器中隐藏代码,并希望向您显示布局
View.isInEditMode()
指示此视图当前是否处于编辑模式。一个观点是
在开发人员工具中显示时,通常处于编辑模式。对于
实例,如果此视图是由可视化用户界面绘制的
在生成器中,此方法应返回true。子类应该检查
返回此方法的值,以提供不同的行为(如果
正常行为可能会干扰主机环境。对于
实例:该类在其构造函数图形中生成一个线程
代码依赖于特定于设备的功能等。此方法通常是
签入自定义小部件的绘图代码
我使用的是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);
}
}
@覆盖
受保护的void onDraw(画布){
super.onDraw(帆布);
/ttoDo:考虑将这些变量存储为成员变量以减少
//每个提款周期的分配。
int paddingLeft=getPaddingLeft();
int paddingTop=getPaddingTop();
int paddingRight=getPaddingRight();
int paddingBottom=getPaddingBottom();
int w=getWidth()-paddingLeft-paddingRight;
int h=getHeight()-paddingTop-paddingBottom;
感谢@Blundell修改我的问题