以编程方式向Android中的framelayout添加片段

编程入门 行业动态 更新时间:2024-10-26 04:25:55
本文介绍了以编程方式向Android中的framelayout添加片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试构建一个既包含静态元素又包含动态元素的UI.为此,我将活动分为多个片段-然后,所有应用程序的导航都是通过替换片段而不是在活动之间进行导航来完成的.

I am trying to build a UI combining both static and dynamic elements. For this, I have divided my activity into fragments - all app navigation is then done by replacing fragments instead of navigating between activities.

在我的主要活动布局中,我使用的是FrameLayout:

In my main activity layout, I am using a FrameLayout:

<FrameLayout android:id="@+id/mainframe" android:layout_height="match_parent" android:layout_width="match_parent" android:layout_below="@id/topsection" android:layout_above="@id/lowersection" />

我有一个这样声明的片段:

I have a fragment declared as such:

public class MyFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragmentlayout, container, false); } }

然后,在我的主要活动(扩展FragmentActivity并使用导入android.support.v4.app.FragmentActivity)中,我试图将此片段加载到框架布局中.

Then, in my main activity (which extends FragmentActivity and uses the import android.support.v4.app.FragmentActivity, I am attempting to load this fragment into the frame layout.

MyFragment myf = new MyFragment(); FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.add(R.id.mainframe, myf); transactionmit();

我在许多其他示例中都遵循了此规则,但是我在transaction.add()命令上收到了一个编译器错误,似乎没有其他人遇到过该错误.

I have followed this from many other examples, however I am receiving a compiler error on the transaction.add() command, which nobody else seems to have encountered.

我收到的错误是:The method add(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, MyFragment).

这是为什么? MyFragment类扩展了Fragment,所以我认为这会起作用.我在做什么错了?

Why is this? The MyFragment class extends Fragment so I would've thought this would work. What am I doing wrong?

我主要活动的进口是:

import org.joda.time.DateTime; import android.app.FragmentTransaction; import android.database.Cursor; import android.os.Bundle; import android.os.Handler; import android.support.v4.app.FragmentActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.TextView; import android.widget.Toast;

推荐答案

检查您的导入.使用android.support.v4.app.FragmentTransaction代替android.app.FragmentTransaction.

Check your imports. Use android.support.v4.app.FragmentTransaction instead of android.app.FragmentTransaction.

此外,请确保您正在使用android.support.v4.app.Fragment并调用getSupportFragmentManager().很容易错过此调用/导入.在FragmentManager的提示下,对saiful103a表示感谢.

Furthermore be sure you are using android.support.v4.app.Fragment and calling getSupportFragmentManager(). It's easy to miss this calls / imports. Thx to saiful103a with the hint of the FragmentManager.

更多推荐

以编程方式向Android中的framelayout添加片段

本文发布于:2023-11-12 18:45:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582246.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:片段   方式   Android   framelayout

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!