How can I define a fragment's layout from XML?

后端 未结 2 1185
萌比男神i
萌比男神i 2021-01-01 15:37

I\'m trying to define a fragment\'s layout in XML in the same way that I defined the layout of my view.

Is this possible? I tried several things, but none of them se

2条回答
  •  隐瞒了意图╮
    2021-01-01 16:06

    A Fragment works much like an activity, in that you need a Java class file to go with it. you cannot create a Fragment just by creating a fragment layout - you need a class for your fragment:

    1. Create a layout XML and an Activity subclass for your activity
    2. Create a layout XML and a Fragment subclass for your fragment
    3. Tie the two together in your Activity layout XML (or using FragmentTransaction if you want to do it in Java code)

    If you haven't already done so, read, re-read and digest everything on this page: https://developer.android.com/guide/components/fragments.html

    There's a lot there, but Fragments are an essential part of Android apps now so it's required reading. The good news is that the basics of fragments is pretty simple.

提交回复
热议问题