java - 如何将按钮添加到android studio中的默认 map 项目?

标签 java android xml google-maps android-fragments

我已经打开了默认的“google map Activity ”,android studio 中的 Activity 只是为了给自己学习一些东西,问题是它在全屏上打开一个 XML fragment ,并在其中放置一个 map View , 我想添加按钮,这意味着添加另一个 fragment 或某种 xml 布局,我该怎么做? 另外我需要在 Java 文件中更改什么才能调用新的 map \按钮。 ?

谢谢!

这是应用程序的主要

package greenroadproject.greenroadproject2;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.GoogleMap;
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 MapsActivity extends FragmentActivity {

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.

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

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    /**
     * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
     * installed) and the map has not already been instantiated.. This will ensure that we only ever
     * call {@link #setUpMap()} once when {@link #mMap} is not null.
     * <p/>
     * If it isn't installed {@link SupportMapFragment} (and
     * {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
     * install/update the Google Play services APK on their device.
     * <p/>
     * A user can return to this FragmentActivity after following the prompt and correctly
     * installing/updating/enabling the Google Play services. Since the FragmentActivity may not
     * have been completely destroyed during this process (it is likely that it would only be
     * stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this
     * method in {@link #onResume()} to guarantee that it will be called.
     */
    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    /**
     * This is where we can add markers or lines, add listeners or move the camera. In this case, we
     * just add a marker near Africa.
     * <p/>
     * This should only be called once and when we are sure that {@link #mMap} is not null.
     */
    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }
}

并且它使用这个 xml 布局 >

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapsActivity" />

如果您只是在 android studio 中打开正常的谷歌地图 Activity ,也可以找到它,它是相同的代码。

那么我如何向所有这些添加按钮\使 map 小一点?

感谢分配

sv

最佳答案

LinearLayout为父级放置 map fragment 以及父级LinearLayout上的按钮。

<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"
    android:orientation="vertical"
    android:gravity="center">

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        tools:context=".MapsActivity" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

关于java - 如何将按钮添加到android studio中的默认 map 项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29999163/

相关文章:

Java:以编程方式从 Trust Store 读取 SSL 证书信息

Java抽象类实现接口(interface)

android - 无法在android中使用videoview播放rtsp

java - 具有不同 xml 名称的相同响应对象

c# - 从 xml 文件中检索元数据

xml - Prolog 中不允许使用 Groovy XmlSlurper 内容

java - 我有一个抽象类和两个扩展,我可以有一个通用的映射器吗?

java - JPA @ManyToMany 关联似乎只能以一种方式工作

java - GestureDetector - 方法 onDown 但在左侧

安卓布局 : Can't place the elements where I want