在Android中使用fragment和frameLayout有什么区别?两者可以互换使用吗? [英] What can be the difference of using a fragment and frameLayout in android? Can both be used interchangeably?

查看:647
本文介绍了在Android中使用fragment和frameLayout有什么区别?两者可以互换使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了一种在出现碎片的情况下使用frameLayout的方法.最终目标是要有多个碎片.

I have seen an approach where frameLayout is used in case of fragments. The ultimate goal was to have multiple fragments.

推荐答案

要在屏幕上立即显示单个片段,可以,您可以互换使用片段或FrameLayout.

For showing a single Fragment immediately on the screen, yes, you can use fragment or FrameLayout interchangeably.

通过fragment标签显示Fragment在XML中看起来像这样:

Showing the Fragment via the fragment tag would look like this in XML:

<fragment class="com.example.ExampleFragment"
        android:id="@+id/details" android:layout_weight="1"
        android:layout_width="0px" android:layout_height="match_parent" />

单个片段,方法2

通过FrameLayout显示片段在XML中看起来像这样:

Single Fragment, Method 2

Showing the Fragment via FrameLayout would look like this in XML:

<FrameLayout android:id="@+id/details" android:layout_weight="1"
            android:layout_width="0px" android:layout_height="match_parent" />

后跟如下Java代码:

Followed by Java code like this:

Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.details, newFragment);
transaction.addToBackStack(null);
transaction.commit();

多个片段

然后,方法2通过运行更多的Java代码来更改以后显示的片段,从而支持更改以后显示的片段:

Multiple Fragments

Method 2 then supports changing what fragment you are showing later by running more Java code to change what Fragment is there afterwards:

Fragment secondFragment = new SecondExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.details, secondFragment);
transaction.addToBackStack(null);
transaction.commit();

因此,FrameLayout为您提供了比使用fragment标签更强大的功能.

So FrameLayout gives you the extra ability to do that over using the fragment tag.

这篇关于在Android中使用fragment和frameLayout有什么区别?两者可以互换使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆