Activity Layout:Fragment 类:vs android:name 属性 [英] Activity Layout: Fragment class: vs android:name attributes
问题描述
我已经阅读了 Android Developer 中关于 Fragments 的文档Guide 和我已经看到,有时他们使用 Fragment 标记属性 android:name 指定要实例化的类,有时他们使用 class: 属性:
I've read the documentation about Fragments in the Android Developer Guide and I've seen that sometimes they specify the class to instantiate with the Fragment tag attribute android:name and sometime they use the class: attribute:
<fragment
android:name="com.example.news.ArticleReaderFragment"
android:id="@+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment
class="com.example.android.apis.app.FragmentLayout$TitlesFragment"
android:id="@+id/titles"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent" />
android:name 和 class: 可以互换吗?如果我在 Eclipse 中使用自动完成功能,它们都显示相同的文档提示(即属性提供要实例化的类名).当要实例化的类的名称与 java 文件名不同时,您可能必须使用第二个,例如 TitlesFragment 位于 FragmentLayout.java 文件中?或者我可以将语法 package.fileDOTjava$Class 也与 android:name 属性一起使用吗?
Are android:name and class: interchangeable? If I use the autocompletion function in Eclipse, they both show the same documentation tip (i.e. the attribute provides the class name to be instantiated). Maybe you must use the second one when the class to be instantiated has a name which is different from the java file name, like TitlesFragment which is in the FragmentLayout.java file? Or can I use the syntax package.fileDOTjava$Class also with the android:name attribute?
我想要一些关于 XML 标签和属性的文档,就像 Android Java 类一样(我已经在另一个 问题).
I'd like to have some documentation for XML tags and attributes as for Android Java Classes (I've asked about it in another question).
推荐答案
正如 Activity.onCreateView 源码所说:
As Activity.onCreateView source says:
String fname = attrs.getAttributeValue(null, "class");
TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.Fragment);
if (fname == null) {
fname = a.getString(com.android.internal.R.styleable.Fragment_name);
}
这似乎意味着程序首先看起来是类"属性.并在失败时看起来名称"属性.因此,如果更有效,则使用类"是正确的.
That seemingly means that program looks "class" attribute first. And on fail looks "name" attribute. So as far as it's true using "class" if more efficient.
这篇关于Activity Layout:Fragment 类:vs android:name 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!