0

我试图在 Fragment 的 onActivityCreated 的 relativeLayout 中添加 MapView (org.osmdroid.views.MapView)。但它不起作用。此 Fragment 由 ShrlockFragmentActivity 启动。我怎样才能让它工作?

public class CommuneFragment extends SherlockFragment {

Context mContext = getActivity();

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState) ;
    return inflater.inflate(R.layout.commune_map_fragment, container, false);
}


@Override
public void onActivityCreated(final Bundle savedState) {
    super.onActivityCreated(savedState);
    mContext=getActivity();

    View mView = getView();

    //Create the OSM view
    MapView mapView = new MapView(mContext, null);
    //enable zoom in and out on the OSM
    mapView.setClickable(true);
    mapView.setBuiltInZoomControls(true);

    RelativeLayout RL= (RelativeLayout) getActivity().findViewById(R.id.rl);
    RL.addView(mapView);
  }
4

3 回答 3

2

那是因为在添加相对布局之前,您没有为地图视图提供宽度和高度:

RelativeLayout RL= (RelativeLayout) getActivity().findViewById(R.id.rl);
    RL.addView(mapView);

您所需要的只是为地图视图提供宽度和高度:

this.mMapView.setLayoutParams(new LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

解决了我的类似问题。谢谢

于 2013-12-18T11:45:27.057 回答
0

我在这个问题中找到了答案: OSMDroid simple example required

但我不知道它不适用于 addView

于 2012-12-21T10:04:39.547 回答
0

您可以在 onCreateView 方法中简单地返回地图视图。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
//init mapView...
return mapView;
}

然后,在您进行片段事务的片段活动中,将此视图放在内容框架布局上。

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.container, new CommuneFragment());
            ft.commit();

对不起我的英语,我希望它有所帮助。

于 2013-11-15T10:52:08.427 回答