谷歌地图右上角的地方按钮API

问题描述:

我无法弄清楚如何在谷歌地图上的屏幕右上角放置一个按钮我是Android开发新手,无法找到直接解释这个怎么做。我的主要活动代码如下所示:谷歌地图右上角的地方按钮API

import android.os.Bundle; 
     import android.support.v7.app.AppCompatActivity; 

     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.LatLngBounds; 
     import com.google.android.gms.maps.model.MarkerOptions; 

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback { 
    private final LatLng LOWER_WEST_BOUND = new LatLng(45.427882, 13.347119); 
    private final LatLng UPPER_EAST_BOUND = new LatLng(46.904552, 16.664808); 
    private final LatLng LJUBLJANA = new LatLng(46.052771, 14.503602); 
    private final LatLngBounds SLOVENIJA_OUTER_BOUNDS = new LatLngBounds(LOWER_WEST_BOUND, UPPER_EAST_BOUND); 

    private GoogleMap mMap; 

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

     // 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); 
    } 


    /** 
    * Manipulates the map once available. 
    * This callback is triggered when the map is ready to be used. 
    * This is where we can add markers or lines, add listeners or move the camera. In this case, 
    * we just add a marker near Sydney, Australia. 
    * If Google Play services is not installed on the device, the user will be prompted to install 
    * it inside the SupportMapFragment. This method will only be triggered once the user has 
    * installed Google Play services and returned to the app. 
    */ 
    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 

     mMap.setLatLngBoundsForCameraTarget(SLOVENIJA_OUTER_BOUNDS); 
     mMap.addMarker(new MarkerOptions().position(LJUBLJANA).title("Marker in Ljubljana")); 
     mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LJUBLJANA, 15)); 
    } 

} 

所需的应用程序的外观:

https://i.stack.imgur.com/n2FXF.png

我.xml文件看起来是这样的:

<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="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/map" 
    tools:context="com.fri.studentskaprehrana.MapsActivity" 
    android:name="com.google.android.gms.maps.SupportMapFragment" 
    tools:layout="@layout/place_autocomplete_fragment" /> 
+0

谢谢你指着我重复,加之谁帮我个人的帮助我能找到解决方案。 – Clutchy

要放置图标的位置在操作栏中。若要将菜单项出现,指the menu docs

对于菜单,请尝试:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto"> 

    <item 
     android:id="@+id/action_button" 
     android:icon="@drawable/buttonIcon" 
     app:showAsAction="always" 
     android:title="Button title" /> 
</menu> 
+0

我仍然不确定如何在我的xml文件中添加菜单项。 – Clutchy

+0

编辑,看看 – rhari

+0

app:showAsAction =“always”会抛出一个错误,我应该在哪里放这个 – Clutchy

很简单。
将菜单添加到您拥有该片段的活动,并且您将在该处有按钮。仅添加一个菜单项。

它非常简单和容易

<?xml version="1.0" encoding="utf-8"?> 
<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" 
    android:id="@+id/rr" 
    > 


    <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="com.fri.studentskaprehrana.MapsActivity" /> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/a100dp" 
     android:orientation="horizontal" 
     android:gravity="right" 
     android:id="@+id/lll"> 


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

</RelativeLayout> 

与XML格式的代码替换所有代码。你可以证明,如果你想

编辑:如果你想要像菜单栏,您可以使用此设计:

<?xml version="1.0" encoding="utf-8"?> 
<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" 
    > 


    <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="com.fri.studentskaprehrana.MapsActivity" /> 


    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="60dp" 
     android:background="#322929"> 


     <ImageView 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:id="@+id/imageView40" 
      android:layout_centerVertical="true" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentEnd="true" 
      android:layout_marginRight="20dp" 
      android:background="#72c33c" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="Your text" 
      android:id="@+id/textView111" 
      android:layout_centerVertical="true" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:layout_marginLeft="10dp" 
      android:textColor="#ffffff" /> 
    </RelativeLayout> 

</RelativeLayout> 
+0

这会将该按钮置于操作栏下方,同时@ dimen/a100dp也会给我一个错误。 – Clutchy

+1

改为使用100dp @ dimen/a100dp。您应该选择使用操作栏或创建一个基本布局,如我的答案。如果你想使用操作栏,其他答案是你的解决方案 –