java Android:如何将片段加载到 FrameLayout
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44292712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: how to load fragment into FrameLayout
提问by André Luiz
I'm starting with Android and in my project I'm using this BottomBar. Love it, works pretty well. The code of my application is almost identical on to the one he uses in the tutorial, the only different thing is that my MainActivity
extends from AppCompatActivity
.
我从 Android 开始,在我的项目中我使用了这个BottomBar。很喜欢,效果很好。我的应用程序的代码与他在教程中使用的代码几乎相同,唯一不同的是我的MainActivity
扩展自AppCompatActivity
.
Now what I'm trying to do is to load fragments in the FrameLayout
:
现在我要做的是在以下位置加载片段FrameLayout
:
<!-- This could be your fragment container, or something -->
<FrameLayout
android:id="@+id/contentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottomBar"
/>
To do so I'm trying this piece of code, which I found googling for the answer of my issue:
为此,我正在尝试这段代码,我发现在谷歌上搜索我的问题的答案:
// Create new fragment and transaction
QrCodeFragment newFragment = new QrCodeFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.bottomBar, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
The line transaction.replace(R.id.bottomBar, newFragment);
is complaining about the type of newFragment, which should be android.app.Fragment
. My QrCode class: public class QrCodeFragment extends Fragment
该行transaction.replace(R.id.bottomBar, newFragment);
抱怨 newFragment 的类型,应该是android.app.Fragment
. 我的 QrCode 类:public class QrCodeFragment extends Fragment
As I'm starting with Android today I'm confused if I'm doing the right thing. If I should really load fragments inside the FrameLayout
and if so, what am I doing wrong that I can't load it. I know it's the type of my fragment, but I don't know how to create it with the required type. I created it using Android Studio > New > Fragment > Fragment (Blank)
当我今天开始使用 Android 时,我很困惑我是否在做正确的事情。如果我真的应该在里面加载片段FrameLayout
,如果是这样,我做错了什么,我无法加载它。我知道这是我的片段的类型,但我不知道如何使用所需的类型创建它。我创建它使用Android Studio > New > Fragment > Fragment (Blank)
Thanks for any help
谢谢你的帮助
UPDATE: I found the solution to my error in this postbut even not having the error I still can't see the fragment.
更新:我在这篇文章中找到了我的错误的解决方案,但即使没有错误我仍然看不到片段。
回答by AlexTa
First you have a mistake in your Fragment transaction line, according with your layout should be:
首先你在你的 Fragment 交易行中有一个错误,按照你的布局应该是:
transaction.replace(R.id.contentContainer, newFragment); // not R.id.bottomBar
Second, you should use supportFragmentManagerinstead of fragmentManagerto work with support fragments, so implement the following way:
其次,您应该使用supportFragmentManager而不是fragmentManager来处理支持片段,因此实现以下方式:
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.contentContainer, newFragment);
transaction.addToBackStack(null);
transaction.commit();