Java Android Studio 布局编辑器无法呈现自定义视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16592965/
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
Android Studio layout editor cannot render custom views
提问by Naetmul
In Android Studio, the layout editor cannot preview custom views in xml.
在 Android Studio 中,布局编辑器无法预览 xml 中的自定义视图。
Very simple example:
非常简单的例子:
public class MyCustomView extends FrameLayout {
public MyCustomView(Context context) {
super(context);
}
public MyCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<com.myprojectxxx.view.MyCustomView
android:layout_width="48dp"
android:layout_height="48dp" />
</LinearLayout>
Android Studio always says,
Android Studio 总是说,
Rendering Problems
The following classes could not be found:
- com.myprojectxxx.view.MyCustomView (Fix Build Path, Create Class)
Tip: Try to build the project
渲染问题
找不到以下类:
- com.myprojectxxx.view.MyCustomView(修复构建路径,创建类)
提示:尝试构建项目
Of course, I HAVE that class. If I click "Create Class", it complains that the same class already exists. If I rebuild that project, nothing changes.
当然,我有那个课。如果我点击“创建类”,它会抱怨同一个类已经存在。如果我重建那个项目,什么都不会改变。
And, yes, the project works very well on my Android device. Also, it is rendered very well in Eclipse ADT. However, in Android Studio, it always says that "CLASSES COULD NOT BE FOUND."
而且,是的,该项目在我的 Android 设备上运行良好。此外,它在 Eclipse ADT 中呈现得非常好。但是,在 Android Studio 中,它总是说“找不到类”。
Android Studio does not have the ability to preview a xml file with custom views? What's wrong with this?
Android Studio 无法使用自定义视图预览 xml 文件?这有什么问题?
回答by Naetmul
Should be fixed by the following commit.
应该通过以下提交修复。
回答by yvolk
As I found out today, at last, the "Classdef not found" etc. errors during layout rendering are actually misleading. What they really mean is that there is someerror during execution of your widget.
正如我今天发现的,最后,布局渲染过程中的“Classdef not found”等错误实际上具有误导性。它们的真正意思是在执行小部件期间出现一些错误。
The simplest way to find out, where exactly the problem lays, is this:
找出问题究竟出在哪里的最简单方法是:
In your XML layout file replace you custom view class (let's call it "MyFrameLayout" for clarity) with Android stock class ( e.g. with FrameLayout) and make sure that Layout Editor works. Add "tools:..." attributes to allow you to see content, not an empty layout. E.g. if you have EditText widget in your custom view, add this attribute to it, which will be used in Design mode only:
tools:text="Sample content"
在您的 XML 布局文件中,将您的自定义视图类(为了清楚起见,我们称之为“MyFrameLayout”)替换为 Android 股票类(例如使用 FrameLayout),并确保布局编辑器正常工作。添加“工具:...”属性以允许您查看内容,而不是空布局。例如,如果您的自定义视图中有 EditText 小部件,请向其添加此属性,该属性将仅在设计模式下使用:
tools:text="Sample content"
("tools: namespace is added by Android Studio automatically)
("tools: 命名空间是由 Android Studio 自动添加的)
- Return your original class name (e.g. "MyFrameLayout") to the XML layout. Does it work now?
- 将您的原始类名(例如“MyFrameLayout”)返回到 XML 布局。现在有效吗?
If not:
如果不:
Copy definition of your custom view to a temporary new class (e.g. "MyFrameLayoutBeforeFix"), for convenience. You will use it for comparison with "MyFrameLayout" class, which you will start modifying now.
Recreate your "MyFrameLayout" class from scratch, using Android Studio, starting with absolute minimum: it should compile. As a result the Java class will contain "extends "FrameLayout" and required constructors/methods e.g. in this case:
package com.myprojectxxx.view; import android.content.Context; import android.util.AttributeSet; import android.widget.FrameLayout; public class MyFrameLayout extends FrameLayout { public MyFrameLayout(Context context) { super(context); } public MyFrameLayout(Context context, AttributeSet attrs) { super(context, attrs); } public MyFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } }
Make sure that this custom view renders normally. It should, at least in year 2016!
Move code piece by piece from a "MyFrameLayoutBeforeFix" copy to this class, checking that there are no errors at each step...
为方便起见,将自定义视图的定义复制到临时的新类(例如“MyFrameLayoutBeforeFix”)。您将使用它与“MyFrameLayout”类进行比较,现在您将开始修改它。
使用 Android Studio 从头开始重新创建“MyFrameLayout”类,从绝对最小值开始:它应该编译。因此,Java 类将包含“扩展“FrameLayout”和所需的构造函数/方法,例如在这种情况下:
package com.myprojectxxx.view; import android.content.Context; import android.util.AttributeSet; import android.widget.FrameLayout; public class MyFrameLayout extends FrameLayout { public MyFrameLayout(Context context) { super(context); } public MyFrameLayout(Context context, AttributeSet attrs) { super(context, attrs); } public MyFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } }
确保此自定义视图正常呈现。它应该,至少在 2016 年!
将代码从“MyFrameLayoutBeforeFix”副本逐个移动到此类,检查每一步都没有错误...
Above sequence seems obvious but it worked for me. The trick is that Layout Editor starts your class in its own context, and this can cause some unexpected errors in your code, which "works" when started from inside your application...
上面的顺序似乎很明显,但它对我有用。诀窍是布局编辑器在其自己的上下文中启动您的类,这可能会导致您的代码中出现一些意外错误,当从您的应用程序内部启动时,这些错误“有效”......
Another trick is to use isInEditMode()
check in your widget's code to skip parts, which may not work in Design view. E.g.:
另一个技巧是使用isInEditMode()
检查小部件的代码来跳过部分,这在设计视图中可能不起作用。例如:
MyClass myClass = isInEditMode() ? null : MyClass.getInstance();
MyClass myClass = isInEditMode() ? null : MyClass.getInstance();
回答by SleepyTonic
Facing the same issue, I had to override the three and four argument constructors:
面对同样的问题,我不得不重写三个和四个参数的构造函数:
public View(Context context, AttributeSet attrs, int defStyle)
public View(Context context, AttributeSet attrs, int defStyle, int defStyleRes)
public View(Context context, AttributeSet attrs, int defStyle)
public View(Context context, AttributeSet attrs, int defStyle, int defStyleRes)
Then rebuild the project.
然后重建项目。
回答by Badamchi
Custom view components are also supported and shown correctly in IDEA, But since IntelliJ IDEA uses class files from your output directory to render such components, you have to do build->make project on your project first.
IDEA 也支持并正确显示自定义视图组件,但由于 IntelliJ IDEA 使用输出目录中的类文件来呈现此类组件,因此您必须先在项目上执行 build->make project。