我正在使用Android studio 2.1.2。我已经检查过,大多数问题要么使用旧版本的Android工作室,要么使用一些不适用于我的情况的旧课程。
来自档案>新项目>我使用了Google Maps Activity选项。我没有更改其中的任何默认代码。它的XML有一个片段。现在有没有办法通过Fragment将这个活动引入另一个Activity?基本上我正在构建一个应用程序,在屏幕的中间部分显示一个地图。这个中间部分我希望是一个片段。底部和顶部区域有一些组件供用户进行交互。
我并不是想让地图出现在新的活动中,而是留在特定活动的中间部分。
或者我只是将新的UI组件添加到默认的Google地图活动中。
首先,我想到了创建一个Fragment(带有自己的.java文件,XML Layout),然后将地图代码放在那里并将其转移到我想要的Activity。完成这项工作的最佳方法是什么?我感谢任何帮助。
答案 0 :(得分:6)
我已经意识到,无论哪种方式,都可以获得相同的结果,一个Activity内的地图,以及其他UI组件。是否使用了Google地图活动,或者是否创建了空活动并在其中实施了地图代码。我的代码包含在下面。我选择了一个空活动(Android 2.1.2),然后将地图代码放入.java文件中,并将带有标记的地图片段放在活动的XML布局中。
错误是我使用过
android:name="com.google.android.gms.maps.MapFragment"
<>在XML Layout中,但使用SupportMapFragment声明并初始化了mapFragment(对象实例)。在复制和粘贴Google文档中的代码时出现了混乱。当我试图在这里更新我的帖子时,我发现了错误。正确的做法是如果你使用MapFragment(API级别12及以上)使用它。如果您决定使用SupportMapFragment(低于API级别12),请使用SupportMapFragment。
以下代码是更正。万一有人有同样的问题。
XML布局(activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.myapp.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/teachers_link"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/parent_link"
android:layout_marginLeft="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="340dp"
android:orientation="vertical">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="135dp"
android:layout_marginTop="12dp"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/search_term" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:id="@+id/select_level"
android:entries="@array/select_level"></Spinner>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/select_course"
android:entries="@array/select_course"></Spinner>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_search" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
.java文件(MainActivity.java)
package name;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
map.addMarker(new MarkerOptions()
.position(new LatLng(0, 0))
.title("Marker"));
}
}