2016-11-18 22 views
0

Googleマップのアクティビティで画面の右上にボタンを配置する方法がわかりません。Android開発を初めて使用しているため、まっすぐな説明が見つかりませんこれを行う方法。私の主な活動のコードは次のようになります。Googleマップの右上にボタンを置く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

答えて

1

アイコンを配置する場所は、アクションバーにあります。 、そこにメニュー項目を配置するメニューについて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 = "常に"エラーを投げます。これはどこに置くべきですか? – Clutchy

0

それは簡単です。
フラグメントを持っているアクティビティにメニューを追加すると、そこにボタンが表示されます。 1つのメニュー項目だけを追加します。

1

その非常にシンプルで簡単

<?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

@ dimen/a100dpの代わりに100dpを使用します。アクションバーを使用するか、私の答えのような基本レイアウトを作成するかを選択する必要があります。アクションバーを使用する場合は、他の答えはあなたの解決策です –

関連する問題