且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

安卓:添加一个片段活动

更新时间:2022-12-29 14:53:40

这里的问题是,该片段需要一个容器中,以期ID居住之内。给它一个布局ID将导致您看到的错误。

The problem here is that the fragment needs a container with a view id to reside within. Giving it a layout id will result in the error you saw.

您可以添加片段到你的亲戚布局,但要做到这一点正确,你需要给它指定适当的布局参数,以便它可以放置。这将是容易只是创建一个包装它的内容相对布局内的FrameLayout,然后添加片段存在

You can add the fragment to your relative layout, but to do that properly you'll need to assign it appropriate layout parameters so that it can be placed. It would be easier to just create a FrameLayout within the relative layout that wraps its contents, and then add the fragment there.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <FrameLayout
        android:id="@+id/FragmentContainer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/B1"/>

</RelativeLayout>

然后

fragmentTransaction.add(R.id.FragmentContainer, fragment);