How Can I Define A Fragment's Layout From Xml?
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 seem to work. My ac
Solution 1:
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:
- Create a layout XML and an Activity subclass for your activity
- Create a layout XML and a Fragment subclass for your fragment
- 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.
Solution 2:
Simply add into your <fragment>
tag as property:
tools:layout="@layout/your_layout_xml"
And in your main.xml (or <fragment>
tag container) into your parent container (in this case ListView
):
xmlns:tools="http://schemas.android.com/tools"
Post a Comment for "How Can I Define A Fragment's Layout From Xml?"