首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SupportMapFragment in TabHost

SupportMapFragment in TabHost
EN

Stack Overflow用户
提问于 2015-07-08 19:14:02
回答 1查看 95关注 0票数 0

在我的当前应用程序中,我试图添加选项卡功能。在其中一个选项卡中,我希望显示列表,而在另一个选项卡中,我希望显示地图。在我以前版本的应用程序中,我用SupportMapFragment显示地图。所以我仍然想使用它(不想修改太多的代码)。但是当我试图添加地图片段时,它的显示错误。有人能告诉我我如何做到这一点吗?

这是我的XML:-

代码语言:javascript
复制
 <?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <TabWidget android:id="@android:id/tabs"
        android:layout_width="fill_parent" android:layout_height="wrap_content" />
    <FrameLayout android:id="@android:id/tabcontent"
        android:layout_width="fill_parent" android:layout_height="fill_parent">

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
            <RadioGroup
                android:id="@+id/rg_views"
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true">

                <RadioButton
                    android:id="@+id/rb_normal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/str_rb_normal"
                    android:checked="true" />

                <RadioButton
                    android:id="@+id/rb_satellite"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/str_rb_satellite" />

                <RadioButton
                    android:id="@+id/rb_terrain"
                    android:layout_width="161dp"
                    android:layout_height="wrap_content"
                    android:text="@string/str_rb_terrain" />

            </RadioGroup>
            <fragment xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/map"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/rg_views"
                class="com.google.android.gms.maps.SupportMapFragment"/>
            <TextView
                android:id="@+id/test"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="User Does not have Our App. Tell him/her to Download our app."
                android:textColor="#000000"
                android:textSize="22sp"
                android:background="#60ffffff"
                android:clickable="true"
                android:layout_below="@+id/rg_views"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />

        </RelativeLayout>




        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:background="@android:color/white"
        android:layout_height="match_parent" >
        <!-- Editext for Search -->
        <EditText android:id="@+id/inputSearch"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="Search Contacts.."
            android:textColor="#000000"
            android:inputType="textVisiblePassword"/>
        <ListView
            android:id="@+id/lst_contacts"

            android:choiceMode="singleChoice"
            android:divider="@color/list_divider"
            android:dividerHeight="1dp"
            android:listSelector="@drawable/list_selector"

            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/inputSearch" />

    </RelativeLayout>

    </FrameLayout>
   </LinearLayout>
 </TabHost>

下面是一些用于实现选项卡的java代码。

代码语言:javascript
复制
  TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
  tabHost.setup();
  tabHost.setOnTabChangedListener(this);
  SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
  listViewPhoneBook=(ListView)findViewById(R.id.lst_contacts);
            // add views to tab host
            tabHost.addTab(tabHost.newTabSpec("list").setIndicator("List").setContent(new TabHost.TabContentFactory() {
                public View createTabContent(String arg0) {
                    return listViewPhoneBook;
                }
            }));
            tabHost.addTab(tabHost.newTabSpec("map").setIndicator("Map").setContent(new TabHost.TabContentFactory() {
                public View createTabContent(String arg0) {
                    return listViewPhoneBook;  //if i use mapFragment here its showing error.
                }

错误:-

代码语言:javascript
复制
 Incompaitable type:
 required: android.view.View
 Found:  com.google.android.gms.maps.SupportMapFragment
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-08 20:51:13

你必须改变两件事:

  • 创建所需选项卡的方法是: TabHost.TabSpec spec=tabs.newTabSpec("map"); spec.setContent(R.id.map); spec.setIndicator("Map"); tabs.addTab(spec);
  • 更改RelativeLayout内容的LinearLayout
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31301671

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档