我试图用我定义视图布局的相同方式来定义XML中的一个片段布局。如何从XML定义片段的布局?
这可能吗?我尝试了几件事,但没有一件似乎工作。
我的活动布局如下所示(main.xml中):
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="full.lommeregner.Lommeregnerrv2Activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lommeregnerv2">
</fragment>
</ListView>
现在,因为我不是通过原始的Java代码生成布局的忠实粉丝,我想我的定义片断如下(fragment_lommeregner .xml):
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="full.lommeregner.Lommeregnerrv2Activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lommeregnerv2">
<!-- my content was located here (some textviews, buttons and so on). -->
</fragment>
我做错了什么?我如何通过XML定义片段的布局?
他们并非严格要求,但在很多情况下他们会让你的生活更轻松。 – adamp 2012-02-26 16:28:49
当前自动生成的Eclipse示例使用FragmentTransactions我认为...该视图由静态片段类生成并从函数调用返回。我目前正在研究如何添加自己的选项卡。 – anon58192932 2013-02-06 01:32:59
“Fragment”类的实例如何指定哪个XML文件包含其布局? 'Activity'有'setContentView(R.layout.my_activity_layout)',可以在'onCreate()'中调用。有没有类似的片段? – dinosaur 2016-04-13 05:51:31