Activity Layout:Fragment 类:vs android:name 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10162983/
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
Activity Layout: Fragment class: vs android:name attributes
提问by Gianni Costanzi
I've read the documentation about Fragmentsin the Android Developer Guide and I've seen that sometimes they specify the class to instantiate with the Fragment tag attribute android:nameand sometime they use the class:attribute:
我已经阅读了Android Developer Guide 中有关Fragments的文档,我看到有时他们指定使用 Fragment 标签属性android:name实例化的类,有时他们使用class:属性:
<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" />
Are android:nameand 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 TitlesFragmentwhich is in the FragmentLayout.javafile? Or can I use the syntax package.fileDOTjava$Classalso with the android:nameattribute?
是安卓:名称和类:互换?如果我在 Eclipse 中使用自动完成功能,它们都显示相同的文档提示(即属性提供要实例化的类名)。当要实例化的类的名称与 java 文件名不同时,也许您必须使用第二个,例如FragmentLayout.java文件中的TitlesFragment?或者我可以将语法package.fileDOTjava$Class也与android:name属性一起使用吗?
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).
我想要一些关于 XML 标签和属性的文档,就像 Android Java 类一样(我在另一个问题中询问过它)。
回答by far.be
As Activity.onCreateView source says:
正如 Activity.onCreateView 来源所说:
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.
这似乎意味着程序首先看起来是“类”属性。并在失败时看起来“名称”属性。因此,如果更有效,则使用“类”是正确的。
回答by CommonsWare
Are android:name and class: interchangeable?
android:name 和 class: 可以互换吗?
Presumably, yes. I have only used class
, and that seems to be what most of Google's examples use, but I do see where they use android:name
in some samples. Unfortunately, there is no formal and complete documentation for <fragment>
.
据推测,是的。我只使用了class
,这似乎是大多数 Google 示例使用的内容,但我确实android:name
在某些示例中看到了它们的使用位置。不幸的是,没有关于<fragment>
.
回答by AndroUser
Sorry all experts are here, I may be wrong but as per my knowledge android:nameattribute of fragment is used to find fragment, when we use getFragmentByTag()method of fragmentManager class. also android:classattribute is used to find fragment class which we generally include for static fragment.
抱歉所有专家都在这里,我可能错了,但据我所知,当我们使用fragmentManager 类的getFragmentByTag()方法时,片段的android:name属性用于查找片段。也android:class属性用于查找我们通常为静态片段包含的片段类。
Hope this will help.. thanks
希望这会有所帮助..谢谢