为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

呼唤远方 2019-12-27 15:16:52
我已经阅读了《 Android开发人员指南》中有关Fragments的文档,并且看到它们有时使用Fragment标签属性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中使用自动完成功能,它们都将显示相同的文档提示(即,属性提供要实例化的类名)。当要实例化的类的名称与Java文件名不同时,例如FragmentLayout.java文件中的TitlesFragment,您可能必须使用第二个吗?还是可以将语法package.fileDOTjava $ Class也用于android:name属性?我想要一些XML标记和属性的文档,就像Android Java类一样(我在另一个问题中已经问过)。
查看完整描述

3 回答

?
萧十郎

TA贡献1815条经验 获得超12个赞

正如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”属性。并在失败时显示“名称”属性。因此,使用“类”更为有效。


查看完整回答
反对 回复 2019-12-27
?
一只名叫tom的猫

TA贡献1906条经验 获得超2个赞

android:name和class:是否可互换?


大概是。我只使用了class,这似乎是Google大部分示例所使用的,但是我确实看到了它们android:name在某些示例中的使用位置。不幸的是,没有的正式和完整文档<fragment>。


查看完整回答
反对 回复 2019-12-27
?
慕哥6287543

TA贡献1831条经验 获得超10个赞

抱歉,所有专家都在这里,我可能错了,但是据我所知,当我们使用fragmentManager类的getFragmentByTag()方法时,fragment的android:name属性用于查找片段。还机器人:类属性是用来寻找片断类,我们一般包括静态的片段

希望这会有所帮助..谢谢


查看完整回答
反对 回复 2019-12-27
  • 3 回答
  • 0 关注
  • 564 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信