Reputation: 849
what is the different between this 2 way of adding fragment:
1:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment class="com.example.android.apis.app.FragmentLayout$TitlesFragment"
android:id="@+id/article_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
2:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:class="com.example.android.apis.app.FragmentLayout$TitlesFragment"
android:id="@+id/article_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
i know that based on this article the FrameLayout is:
designed to block out an area on the screen to display a single item
so i want to know that is there any different in their outputs appearance???
Upvotes: 1
Views: 86
Reputation: 2881
There is basically no difference, you're just adding a level of abstraction to your view.
The output will be exactly the same, but with the first solution, you'll be able to add some overlapping views over your fragment (because a FrameLayout
is basically a stack of views (your quote about FrameLayout
is not exact btw, you can add lots of elements in this layout)).
In the second option, you'll only have the possibility to add some views before or after your fragment (horizontally or vertically depending on the orientation of the LinearLayout
you're using)
The FrameLayout
is very cool to make overlapping views, it is even written in your article (I'm a big fan of this method):
You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.
Upvotes: 1
Reputation: 4766
Both of you ways are the same thing, though the FrameLayout is redundant.
FrameLayout is as you said designed to block out an area on the screen to display a single item. And so, it's usually used in this way:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
so that later in runtime, you can replace it with a fragment of your choosing, like in this example:
MyFragment myFragment = MyFragment.newInstance();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragmentContainer, myFragment );
ft.commit();
Upvotes: 1