5

我正在我的 MapActivity.java 中创建一个 OSMdroid 地图视图,我想添加按钮和弹出窗口 - 我只知道这是如何在 .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" />
4

1 回答 1

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 元素的优秀教程/示例,我将非常高兴。

于 2012-11-11T20:47:25.070 回答