0

我有一个带有查看器的操作栏。如果我理解正确,我必须为将在我的操作栏中单击的每个选项卡创建片段。为此,我创建了从 Fragment 继承的 xml 布局文件和类。在onCreateView这些课程中,我返回inflater.inflate(xml_layout, container, false)。Everyting 工作正常,但我的问题如下。

现在我希望这些片段中的一个是包含地图 V2 片段的布局。

实际上我希望我的片段看起来像下面的 xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_mappart"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/grey" >

<LinearLayout
    android:id="@+id/map_heading"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <View
        android:background="@drawable/rectangle_shape"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="50dp" />

    <TextView 
        android:id="@+id/map_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:layout_gravity="bottom"
        android:textSize="16sp"
        android:layout_marginLeft="10dp"
        android:layout_marginBottom="2dp" />

</LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/horizontal_layout_map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_marginBottom="50dp" >

    <View 
        android:background="@drawable/rectangle_shape_yellow"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginTop="250dp" />

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mapfragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />

</LinearLayout>

<View
    android:background="@drawable/rectangle_shape"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginLeft="175dp"
    android:layout_marginTop="-50dp" />

    </LinearLayout>

现在,当我创建一个从 Fragment 扩展并扩展此 xml 的类时,它在我的地图 V2 片段开始的 xml 行上给了我一个错误。

我想说,当我从 SupportMapFragment 扩展并返回super.onCreateView()他自己的 onCreateView 时,一切正常,但是我无法控制地图周围的布局。我希望将地图嵌套到我自己的特定布局中。所以,我几乎可以肯定everyting 设置为displat maps V2,但问题确实是我不能将片段膨胀为片段。

我希望我解释得足够好,如果有人可以帮助我,非常感谢!

4

1 回答 1

0

不幸的是,您不能嵌套片段,这是您在这里尝试做的。这将是一个方便的功能,我可以在我自己的应用程序中使用它,但要绕过这个限制可能很复杂。

一个视图中的片段可以在同一父层次结构中的不同视图中实例化片段,但它不能在其自己的视图层次结构中实例化片段。

有很多方法可以做你想做的事;你只需要考虑你的设计。

于 2013-01-31T21:48:24.477 回答