【问题标题】:How to include FragmentActivity layout in Activity layout?如何在 Activity 布局中包含 FragmentActivity 布局?
【发布时间】:2015-12-03 17:16:44
【问题描述】:

我想在另一个 AppCompatActivity 布局中包含一个 FragmentActivity 布局。

我特别想在另一个 AppCompatActivity 的容器内显示 Google 地图(在本例中为 FragmentActivity)。 我要展示我的代码。

1. AppCompatActivity 布局:

<LinearLayout 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">

    <include
        android:id="@+id/map_toolbar"
        layout="@layout/app_bar" />

    <LinearLayout
        android:id="@+id/map_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    </LinearLayout>
</LinearLayout>

2.AppCompatActivity 类

public class MapActivity extends AppCompatActivity {

    private Toolbar _toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        _toolbar = (Toolbar) findViewById(R.id.map_toolbar);
        setSupportActionBar(_toolbar);
    }
}

3.FragmentActivity 布局:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

4.FragmentActivity 类:

public class MapFragmentActivity extends FragmentActivity implements OnMapReadyCallback
{

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_map);

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

    }
}

当我运行它时,只需在顶部显示应用栏,并在我需要地图的地方显示空白框。

请帮忙。

谢谢。

【问题讨论】:

    标签: android android-fragments android-activity android-fragmentactivity


    【解决方案1】:

    我在您的MapActivity 中看不到您的地图布局。此外,您在FragmentActivity 而不是Fragment 中使用您的地图。我就是这样做的。

    public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {
        private Toolbar _toolbar;
        private GoogleMap mGoogleMap;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_map);
            _toolbar = (Toolbar) findViewById(R.id.map_toolbar);
            setSupportActionBar(_toolbar);
    
            initMap();
        }
    
        public void initMap() {
            MapFragment map = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
            map.getMapAsync(this);
        }
    
        @Override
        public void onMapReady(GoogleMap googleMap) {
            try {
                if (googleMap != null) {
                    mGoogleMap = googleMap;                             
                    mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);               
                }
            } catch (Exception e) {
                e.printStackTrace();
                Log.e("ERROR", "GOOGLE MAPS NOT LOADED");
            }
        }
    }
    

    然后我会在我的 XML 中这样做:

    <LinearLayout 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">
    
        <include
            android:id="@+id/map_toolbar"
            layout="@layout/app_bar" />
    
        <LinearLayout
            android:id="@+id/map_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <fragment
               android:id="@+id/map"
               android:name="com.google.android.gms.maps.SupportMapFragment"
               android:layout_width="match_parent"
               android:layout_height="match_parent" />
    
        </LinearLayout>
    </LinearLayout>
    

    那么您甚至不需要 MapFragmentActivity,因为它有点多余。此外,您现在正在等待地图准备好,然后再使用implements OnMapReadyCallback 显示它。这将在以后为片段准备好之前为片段充气,从而为您省去一些麻烦。

    【讨论】:

    • 是的,但是 FragmentActivity 类中的 onCreate 呢?
    • 你不需要 FragmentActivity 类,它是多余的,这样你就可以直接从你的 MapActivity 控制一切,这样你就不会遇到 Context 的问题。 MapFragment 是为您提供的用于此确切目的的东西。显示谷歌地图
    • 好吧,在没有 FragmentActivity 的情况下,我乞求实施这样的解决方案时也有同样的想法。但我认为是黑客。我更喜欢文档中的明确解决方案
    • 这一切发生是因为我想包含需要AppCompatActivity的工具栏(应用栏)。
    • 不,这绝对不是 hack。这正是谷歌地图的用途。试试看,相信我。我使用 Google Map API 做了很多工作。
    【解决方案2】:

    MapFragment 改为LayoutAppCompatActivity

    <LinearLayout 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">
    
            <include
                android:id="@+id/map_toolbar"
                layout="@layout/app_bar" />
    
            <LinearLayout
                android:id="@+id/map_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                   <fragment xmlns:android="http://schemas.android.com/apk/res/android"
                        android:id="@+id/map"
                        android:name="com.google.android.gms.maps.SupportMapFragment"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
            </LinearLayout>
        </LinearLayout>
    

    【讨论】:

    • 这仍然给了我一个空白片段。片段视图中渲染 FragmenActivity 布局的引用在哪里?
    • 等一下...您是否按照设置Map的初始设置?
    • 是的..我已经拥有的地图是android studio的默认地图。
    • 我的意思是API Key等等。
    • 是的,你是对的......但是是的,我已经这样做了。直接启动地图就可以看到地图了。
    猜你喜欢
    • 2011-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 2021-04-10
    相关资源
    最近更新 更多