gpt4 book ai didi

android - 如何将 fragment 放置到 Activity 的默认根 ViewGroup 中?

转载 作者:行者123 更新时间:2023-11-30 03:44:36 25 4
gpt4 key购买 nike

根据android开发者指南,

To get started, your layout must include a ViewGroup in which you place each Fragment associated with a tab. Be sure the ViewGroup has a resource ID so you can reference it from your tab-swapping code. Alternatively, if the tab content will fill the activity layout (excluding the action bar), then your activity doesn't need a layout at all (you don't even need to call setContentView()). Instead, you can place each fragment in the default root ViewGroup, which you can refer to with the android.R.id.content ID (you can see this ID used in the sample code below, during fragment transactions).

但是如果没有 setContentView(),每当我调用 getActionBar() 时,它都会返回 null。我想知道如何将 fragment 放在 default root ViewGroup 中?我是通过 XML 还是以编程方式添加它?

最佳答案

But without the setContentView(), whenever I call getActionBar(), it returns null

您是在哪些 Android 版本上测试的?如果我没记错的话,这确实发生在 11 和 12 API 级别,但在其他版本上它有效。

为避免复杂化,您应该将内容 View 设置为具有简单 FrameLayout 的布局,并将其用作 fragment 的容器。您也可以尝试只使用 Window.FLAG_ACTION_BAR没有内容 View :

getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

关于android - 如何将 fragment 放置到 Activity 的默认根 ViewGroup 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15193864/

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