我正在尝试以与视图布局相同的方式在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定义片段的布局?
Fragment
类的实例如何指定包含其布局的XML文件?Activity
有setContentView(R.layout.my_activity_layout)
,可以在onCreate()
期间调用。是否有类似于片段的模拟? - dinosaur