Mapview上的按钮

时间:2012-11-11 19:03:10

标签: java android android-layout openstreetmap osmdroid

我在MapActivity.java中创建一个OSMdroid mapview,我想添加按钮&弹出窗口 - 我只知道这是如何在.xml中完成的,但由于这个MapView没有使用任何.xml,我很困惑如何在我的java代码中放置(图像)按钮。

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Setup base map
    final RelativeLayout rl = new RelativeLayout(this);

    CloudmadeUtil.retrieveCloudmadeKey(getApplicationContext());

    final MapView osmv = new MapView(this, 256);

    myMapController = osmv.getController();  

    rl.addView(osmv, new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT));
    osmv.setBuiltInZoomControls(true);
    osmv.setMultiTouchControls(true);

    myLocationoverlay = new MyLocationOverlay(this, osmv);

//*snip* setup of map, mapcontrollers, tiles etc...
    osmv.getOverlays().add(tilesOverlay);
    osmv.getOverlays().add(myLocationoverlay);  

    this.setContentView(rl);
}

编辑: 我在谈论像

这样的按钮
<ImageButton
    android:id="@+id/map_goto_location"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/goto_location"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" 
    android:id="@+id/goto_location" />

1 个答案:

答案 0 :(得分:4)

感谢MH的提示,我发现如何以编程方式将ImageButton添加到我的代码中,如下所示:

    ImageButton goto_location = new ImageButton(this);
    goto_location.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            showMylocation();
        }           
    });

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(40, 40);
    params.rightMargin = 10;
    params.topMargin = 10;
    rl.addView(goto_location, params);

如果有人可以在一些关于自定义按钮的好教程/示例上提示我,并且以编程方式添加UI元素,我将非常高兴。