Java 从MapFragment显示设置窗口

Java 从MapFragment显示设置窗口,java,android,google-maps,google-play-services,Java,Android,Google Maps,Google Play Services,在MapFragment中,我想显示一些设置窗口。最好的方法是什么? 替换碎片?创建某种重叠视图?警报对话框 我如何最好地实现它 btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //... } }); 谢谢将设置放在一个diaglog片段中,从actionbar中的按钮激活,并提供一个界面,以便您可以在后台更新地图 在我的应用程序中查看此live

在MapFragment中,我想显示一些设置窗口。最好的方法是什么? 替换碎片?创建某种重叠视图?警报对话框

我如何最好地实现它

btn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //...
    }
});

谢谢

将设置放在一个diaglog片段中,从actionbar中的按钮激活,并提供一个界面,以便您可以在后台更新地图

在我的应用程序中查看此live

操作栏中的按钮

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >

<item
    android:id="@+id/action_settings"
    android:icon="@drawable/action_settings"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    android:showAsAction="always"
    yourapp:showAsAction="always"/>
做一些有用的事情来展示片段

private void showSettingsDialog() {
    FragmentManager fm = getSupportFragmentManager();
    MapSettings editSettingsDialog = new MapSettings();
    editSettingsDialog.show(fm, "fragment_edit_name");
}
public class KmlReader extends ActionBarActivity implements
    BestRidesSettingsDialogListener, SnapshotReadyCallback,
    OnMapLoadedCallback {



@Override
public void onMapSettingsChange(int mapType) {
    // TODO Auto-generated method stub
    if (mMap != null) {
        mMap.setMapType(mapType);
    }
}
完整的mapsettings类

public class MapSettings extends DialogFragment implements
    OnCheckedChangeListener {

    public static final String MAP_TYPE = "com.gosylvester.bestrides.settings.maptype";
BestRidesSettingsDialogListener activity;
SharedPreferences sharedpref;

public interface BestRidesSettingsDialogListener {
    void onMapSettingsChange(int mapType);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
}

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
         Bundle savedInstanceState) {

    // the activity may be null if this is called without implementing the
     // BestRidesSettingsDialogListener (The settings object saves the
    // setting so the
    // call back may not be needed.

     activity = (BestRidesSettingsDialogListener) getActivity();

     getDialog().setTitle(R.string.app_name);
    View view = inflater.inflate(R.layout.activity_map_settings, container);
    RadioGroup rg = (RadioGroup) view.findViewById(R.id.radioGroup1);
    // initialize to the shared preferences value
    rg.clearCheck();

 ... homemade glue to get the initial setting.      
GoPreferences.getInt(getActivity(),MAP_TYPE,GoogleMap.MAP_TYPE_NORMAL);

    RadioButton rb = null;

     switch (x) {
    case GoogleMap.MAP_TYPE_HYBRID:
         rb = (RadioButton) view.findViewById(R.id.RDOHybrid);
        rb.setChecked(true);
        break;
    case GoogleMap.MAP_TYPE_NORMAL:
        rb = (RadioButton) view.findViewById(R.id.RDORoad);
        rb.setChecked(true);
        break;
    case GoogleMap.MAP_TYPE_SATELLITE:
        rb = (RadioButton) view.findViewById(R.id.RDOSatelite);
        rb.setChecked(true);
        break;
    case GoogleMap.MAP_TYPE_TERRAIN:
        rb = (RadioButton) view.findViewById(R.id.RDOTerrain);
        rb.setChecked(true);
        break;
    }
    // set the listener after setting up
    rg.setOnCheckedChangeListener(this);
    return view;
}   

@Override
public void onCheckedChanged(RadioGroup rg, int checkId) {
    // TODO Auto-generated method stub
    int mapType = 0;
    switch (checkId) {
    case R.id.RDORoad:
        mapType = GoogleMap.MAP_TYPE_NORMAL;
        break;
    case R.id.RDOHybrid:
        mapType = GoogleMap.MAP_TYPE_HYBRID;
        break;
    case R.id.RDOSatelite:
        mapType = GoogleMap.MAP_TYPE_SATELLITE;
        break;
    case R.id.RDOTerrain:
        mapType = GoogleMap.MAP_TYPE_TERRAIN;
        break;
    }
    // run the activity onchange
    // if the activity is null there is no listener to take action on the
    // settings
    if (activity != null) {
        activity.onMapSettingsChange(mapType);
    }

    // save the settings


}
在映射活动上实现接口,以便可以从对话框片段更改映射

private void showSettingsDialog() {
    FragmentManager fm = getSupportFragmentManager();
    MapSettings editSettingsDialog = new MapSettings();
    editSettingsDialog.show(fm, "fragment_edit_name");
}
public class KmlReader extends ActionBarActivity implements
    BestRidesSettingsDialogListener, SnapshotReadyCallback,
    OnMapLoadedCallback {



@Override
public void onMapSettingsChange(int mapType) {
    // TODO Auto-generated method stub
    if (mMap != null) {
        mMap.setMapType(mapType);
    }
}
祝你好运
Danny117

如果你的应用程序有这个按钮,我会把它放在操作栏上。“设置”页面,或者你会称之为“首选项”活动。