gpt4 book ai didi

android - 添加 fragment 的不同方式

转载 作者:行者123 更新时间:2023-11-29 20:38:33 25 4
gpt4 key购买 nike

这两种添加 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>

我知道基于 this article FrameLayout 是:

designed to block out an area on the screen to display a single item

所以我想知道它们的输出外观有什么不同吗???

最佳答案

尽管 FrameLayout 是多余的,但你们两种方式都是一样的。

正如您所说,FrameLayout 旨在遮挡屏幕上的一个区域以显示单个项目。因此,它通常以这种方式使用:

<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>

以便稍后在运行时,您可以将其替换为您选择的 fragment ,如本例所示:

MyFragment myFragment = MyFragment.newInstance();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragmentContainer, myFragment );
ft.commit();

关于android - 添加 fragment 的不同方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31221168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com