2015-05-02 33 views
-1

我選擇了默認的「谷歌地圖活動」,android studio中的活動僅僅是爲了學習一些東西,事情是它在全屏幕上運行一個片段XML並將地圖視圖全部放入, 我想要添加按鈕,意味着添加另一個片段或另一種xml佈局,我該怎麼做? 我還需要在Java文件中更改以調用新地圖\按鈕。 ?如何將按鈕添加到android studio中的默認地圖項目?

thnx!

這是應用程序>

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上正常的谷歌地圖活動工作室這是相同的代碼。

所以我如何添加按鈕到所有這\使地圖有點小?

日Thnx配發

SV

回答

0

LinearLayout父把地圖片段,以及對父母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> 
+0

謝謝,我需要在主java文件中更改什麼? \我如何在線性佈局中按下按鈕? –

0

在XML文件中使用下面的代碼

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#00BCD4" 
    android:id="@+id/main"> 

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

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:text="Button" 
     android:layout_marginTop="40dp" 
     android:padding="8dp" 
     android:background="#536DFE" 
     android:id="@+id/buttonMaps" 
    /> 

在MapsActivity.java您的onCreate方法使用下面的代碼來解決您的按鈕。

final Button button = (Button) findViewById(R.id.buttonMaps); 

要點擊buttonMaps時添加動作,請添加以下代碼。

buttonMaps.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

     //Add your code to be executed when button is clicked 

     } 
}); 
相關問題