【问题标题】:android maps V2 fragment in a fragment does not want to inflateandroid在一个片段中映射V2片段不想膨胀
【发布时间】:2013-01-31 21:29:09
【问题描述】:

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

现在我希望其中一个片段成为包含地图 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 扩展并在他自己的 onCreateView 中返回 super.onCreateView() 时,一切正常,但是我无法控制地图周围的布局。我希望将地图嵌套到我自己的特定布局中。所以,我几乎可以肯定everyting 设置为displat maps V2,但问题是我无法将片段膨胀为片段。

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

【问题讨论】:

    标签: android google-maps fragment


    【解决方案1】:

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

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

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

    【讨论】:

    • 感谢您的快速回答。它的工作原理很奇怪,但是当我第二次单击带有地图片段的选项卡时,它会出现错误。第一次显示地图片段时,它可以工作。你能否举一个例子来说明一种方法来做我想做的事,因为目前我想不出一个......
    • 这是我第一次使用带有标签的操作栏。我找到的所有文档都解释了如何使用片段执行此操作。但也许你知道如何在不使用片段的情况下做同样的事情?谢谢
    猜你喜欢
    • 2014-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多