活动布局:片段类:vs android:name属性

活动布局:片段类:vs android:name属性,android,android-fragments,android-xml,xml-attribute,Android,Android Fragments,Android Xml,Xml Attribute,我已经阅读了《Android开发者指南》中的文档,我看到有时候他们使用片段标记属性Android:name指定要实例化的类,有时候他们使用class:attribute: <fragment android:name="com.example.news.ArticleReaderFragment" android:id="@+id/viewer" android:layout_weight="2" android:layout_width="0dp"

我已经阅读了《Android开发者指南》中的文档,我看到有时候他们使用片段标记属性Android:name指定要实例化的类,有时候他们使用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" />

安卓:名称和类别:可互换吗?如果我在Eclipse中使用autocompletion函数,它们都会显示相同的文档提示(即属性提供要实例化的类名)。如果要实例化的类的名称与java文件名不同,比如FragmentLayout.java文件中的TitlesFragment,那么可能必须使用第二个名称?或者我可以将syntax package.fileDOTjava$类与android:name属性一起使用吗

我想要一些关于XML标记和属性的文档,就像Android Java类一样(我在另一篇文章中问过)

安卓:名称和类别:可互换吗


大概是的。我只使用了
class
,这似乎是谷歌大多数示例所使用的,但我确实看到了他们在一些示例中使用
android:name
。不幸的是,

As 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);
}
这似乎意味着程序首先要查看“class”属性。失败时显示“name”属性。
因此,如果使用“类”更有效,那么使用“类”是正确的。

对不起,所有专家都在这里,我可能是错的,但据我所知,android:name在使用fragmentManager类的getFragmentByTag()方法时,片段的属性用于查找片段。 另外,android:class属性用于查找片段类,我们通常将其包含在静态片段中

希望这会有帮助。。
谢谢

我真希望有文档。由于
class
没有名称空间,我很好奇该属性是否不仅仅是在开发片段时使用的,而是因为它们现在不能破坏兼容性而留在了中。为了保持一致性,
android:name
似乎更可取,正如user1550716所指出的,它们似乎可以互换。@spaarky21:是的,我已经切换到
android:name
无处不在。使用name vs class的附加凭证:IntelliJ IDEA使用/自动填充其最新布局编辑器(v13)中的name属性aapt r20有一个bug,它只影响
class
和@xavier ducrohet推荐的
android:name
。我们需要一个完全限定的类名吗?或者我们可以只
.ArticleReaderFragment
?根据,他们使用
android:tag
属性,这很有意义:
android:tag可以用于为我刚刚签出的片段提供特定的标签名,android Studio 2.0提供的基本模板默认使用
name:xxx
属性。中的android:name属性指定要在布局中实例化的片段类。这是我在开发人员网站中看到的定义。页面:。