Android xml 布局中的自定义视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3260876/
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 view in xml layout
提问by monoceres
I've created my own view by creating a subclass of the SurfaceView class.
我通过创建 SurfaceView 类的子类来创建自己的视图。
However I can't figure out how to add it from the xml layout file. My current main.xml looks like this:
但是我不知道如何从 xml 布局文件中添加它。我当前的 main.xml 如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<View
class="com.chainparticles.ChainView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
What have I missed?
我错过了什么?
Edit
编辑
More info
更多信息
My view looks like this
我的观点是这样的
package com.chainparticles;
public class ChainView extends SurfaceView implements SurfaceHolder.Callback {
public ChainView(Context context) {
super(context);
getHolder().addCallback(this);
}
// Other stuff
}
And it works fine like this:
它像这样工作正常:
ChainView cview = new ChainView(this);
setContentView(cview);
But nothing happens when trying to use it from the xml.
但是尝试从 xml 中使用它时没有任何反应。
回答by Rich Schuler
You want:
你要:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.chainparticles.ChainView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Edit:
编辑:
After seeing the rest of your code it's probably throwing because you can't call getHolderin the constructor while being inflated. Move that to View#onFinishInflate
在看到您的其余代码后,它可能会抛出,因为您无法getHolder在膨胀时调用构造函数。将其移至View#onFinishInflate
So:
所以:
@Override
protected void onFinishInflate() {
getHolder().addCallback(this);
}
If that doesn't work try putting that in an init function that you call in your Activitys onCreate after setContentView.
如果这不起作用,请尝试将其放入您在Activitys onCreate 之后调用的 init 函数中setContentView。
It was probably working before because when inflating from xml the constructor:
View(Context, AttributeSet)is called instead of View(Context).
它之前可能有效,因为当从 xml 膨胀时,构造函数:
View(Context, AttributeSet)被调用而不是View(Context).
回答by nonsleepr
What you missed in your example was the tag name, it supposed to be "view" (first non-capital) not "View". Although you can put your class name as the tag name most of the time, it's impossible to do that if your class is inner class, because "$" symbol, which is used in Java to reference inner classes is restricted in XML tags. So, if you want to use inner class in your XML you should write like this:
您在示例中错过的是标签名称,它应该是“view”(第一个非大写)而不是“View”。虽然大多数时候你可以把你的类名作为标签名,但如果你的类是内部类,这是不可能的,因为在 Java 中用于引用内部类的“$”符号被限制在 XML 标签中。所以,如果你想在你的 XML 中使用内部类,你应该这样写:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<view
class="com.chainparticles.Foo$InnerClassChainView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
The thing is that both "view" and "View" tags exist in the schema. "View" tag (started with capital letter) will generate a View class, while "view" tag, when parsed, will examine the class attribute.
问题是“视图”和“视图”标签都存在于模式中。“ View”标签(以大写字母开头)将生成一个View类,而“ view”标签在解析时会检查类属性。

