java – Android:如何将片段加载到FrameLayout中
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – Android:如何将片段加载到FrameLayout中,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1812字,纯文字阅读大概需要3分钟。
内容图文
![java – Android:如何将片段加载到FrameLayout中](/upload/InfoBanner/zyjiaocheng/751/95d2ccd47b294a07a4b0a4a229e34924.jpg)
我从Android开始,在我的项目中,我正在使用这个BottomBar.喜欢它,效果非常好.我的应用程序的代码与他在教程中使用的代码几乎相同,唯一不同的是我的MainActivity从AppCompatActivity扩展.
现在我要做的是在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"
/>
为此,我正在尝试这段代码,我发现谷歌搜索我的问题的答案:
// 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();
行transaction.replace(R.id.bottomBar,newFragment);抱怨newFragment的类型,应该是android.app.Fragment.我的QrCode类:公共类QrCodeFragment扩展了Fragment
当我今天开始使用Android时,如果我做对了,我会感到困惑.如果我真的应该在FrameLayout中加载片段,如果是的话,我做错了什么我无法加载它.我知道这是我的片段的类型,但我不知道如何使用所需类型创建它.我使用Android Studio创建了它>新>片段>片段(空白)
谢谢你的帮助
更新:
我在this post找到了我的错误的解决方案,但即使没有错误,我仍然看不到片段.
解决方法:
首先,你的Fragment交易行中有一个错误,根据你的布局应该是:
transaction.replace(R.id.contentContainer, newFragment); // not R.id.bottomBar
其次,您应该使用supportFragmentManager而不是fragmentManager来处理支持片段,因此请执行以下方法:
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.contentContainer, newFragment);
transaction.addToBackStack(null);
transaction.commit();
内容总结
以上是互联网集市为您收集整理的java – Android:如何将片段加载到FrameLayout中全部内容,希望文章能够帮你解决java – Android:如何将片段加载到FrameLayout中所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。