在Android中向Google Map添加按钮

时间:2014-04-03 19:52:25

标签: map

我是android新手。我正在做一个项目,作为我学术的一部分。我想在谷歌地图android的底部有几个按钮,就像在waze android应用程序中一样(很抱歉,无法发布屏幕截图)。我根据以下链接尝试了一些东西 How to add buttons at top of map fragment API v2 layout。但没有得到理想的结果。

有人可以帮我正确布局。感谢您的帮助。

3 个答案:

答案 0 :(得分:3)

试试这个:

<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"
    tools:context=".MainActivity" >

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />

    <Button
        android:id="@+id/setRangeButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/setRange" />
</RelativeLayout>

答案 1 :(得分:1)

您可以扩展SupportMapFragment并覆盖OnCreateView以编程方式执行::

public class myMap extends SupportMapFragment{ 

    private Context context; 

    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        // TODO Auto-generated method stub  

        View v = super.onCreateView(inflater, container,savedInstanceState);  

        //save context
        this.context = v.getContext();

        RelativeLayout view = new RelativeLayout(v.getContext());  


       //add relative layout
        view.addView(v, new RelativeLayout.LayoutParams(-1, -1));  

        TextView stuff = new EditText(v.getContext());
        stuff.setText("Hey you, I was added programatically!");


        view.addView(stuff);


        initializeMap();
        return view;  //return 
    }

    private initializeMap(){
        //add things to the map here  
        getMap().addMarker(new MarkerOptions().add(new LatLng(-37.81319, 144.96298))); 
    }
}

答案 2 :(得分:0)

使用此代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <Button 
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="Button 1"/>


</RelativeLayout>