当前位置: 首页 > 知识库问答 >
问题:

将actionbar添加到FragmentActivity

谢承颜
2023-03-14

我正在做一个应用程序,使用谷歌地图,因为一些特定的原因。所以我在Android Studio中创建了一个Google Maps应用程序,我得到了一个扩展FragmentActivity的活动。一切工作都很完美,但现在我想在屏幕顶部添加操作条。问题是地图占了整个屏幕,不知道怎么修复

public class MainActivity extends FragmentActivity implements LocationListener {
    private GoogleMap mMap; // Might be null if Google Play services APK is not available.
    double lon = 0;
    double lat = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setUpMapIfNeeded();

    }
    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }
    public void showSettingsAlert(){
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

        // Setting Dialog Title
        alertDialog.setTitle("GPS settings");

        // Setting Dialog Message
        alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

        // On pressing Settings button
        alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }
        });

        // on pressing cancel button
        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        // Showing Alert Message
        alertDialog.show();
    }
    private void setUpMapIfNeeded() {
        LocationManager manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        /*if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            showSettingsAlert();
        }
        else{*/
        // Getting Google Play availability status
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

        // Showing status
        if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available

            int requestCode = 10;
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
            dialog.show();

        }else { // Google Play Services are available

            // Getting reference to the SupportMapFragment of activity_menu.xml
            SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

            // Getting mMap object from the fragment
            mMap = fm.getMap();

            // Enabling MyLocation Layer of Google Map
            mMap.setMyLocationEnabled(true);

            // Getting LocationManager object from System Service LOCATION_SERVICE
            LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
            boolean networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            if(networkEnabled){
                Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                if(location!=null){
                    lon = location.getLongitude();
                    lat = location.getLatitude();
                    LatLng latLng = new LatLng(lat, lon);
                    // Showing the current location in Google Map
                    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
                    // Zoom in the Google Map
                    mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
                }
            }
            // Creating a criteria object to retrieve provider
            Criteria criteria = new Criteria();

            // Getting the name of the best provider
            String provider = locationManager.getBestProvider(criteria, true);

            // Getting Current Location
            Location location = locationManager.getLastKnownLocation(provider);

            if(location!=null){
                onLocationChanged(location);
            }
            locationManager.requestLocationUpdates(provider, 20000, 0, this);
        /*}*/}
    }


    @Override
    public void onLocationChanged(Location location) {
        double latitude = location.getLatitude();

        // Getting longitude of the current location
        double longitude = location.getLongitude();

        // Creating a LatLng object for the current location
        LatLng latLng = new LatLng(latitude, longitude);

        // Showing the current location in Google Map
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

        // Zoom in the Google Map
        mMap.animateCamera(CameraUpdateFactory.zoomTo(15));

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_menu, menu);
        super.onCreateOptionsMenu(menu);
        return true;
    }
<fragment 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"
android:id="@+id/map"
tools:context=".MainActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"/>

共有1个答案

毋澄邈
2023-03-14

使您的mainactivity改为扩展appcompatactivity

以前应该扩展actionbaractivity,但现在已经不推荐了。

 类似资料:
  • 当我从片段B回到片段A时,actionbar标题应该是“fraga”,但它仍然是“fragb”。问题是add方法片段A没有重新创建,我没有找到事件来刷新它。 目前我找到的唯一简单的解决方案是: 1-fragb.onResume:保存上一个操作栏标题

  • 问题内容: RelativeLayout layout = new RelativeLayout(this); AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); View gameView = initializeForView(new MainGame(), config); 什么也没显

  • 问题内容: 现在,我正在通过jquery加载iframe: 我有一个自定义样式表,希望将其应用于该页面。iframe中的页面不在同一域中,这就是为什么它很棘手的原因。我找到了允许您添加单个标签但不能添加整个样式表的解决方案。 编辑: 有问题的页面通过Mobile Safari 加载,因此跨域策略不适用。 问题答案: 基于解决方案您已经找到了如何将CSS应用于iframe?: 或更多jqueryis

  • 问题内容: 我有一个gui,它的Panel包含一系列标签和TextField,并使用spring布局(这是mainPanel),而另一个Panel仅包含button(buttonPanel)。我正在尝试使我的mainPanel也具有垂直滚动条。我想实现我的GUI,以便在JFrame中有2个面板。mainPanel出现在框架的顶部,而buttonPanel出现在mainPanel的下方。 我的问题是

  • 问题内容: 我正在尝试使用standard共享图像,可以使用以下代码在 Facebook , Twitter 和 Save Image 上 共享: 我还需要一件事, Instagram : 这两个代码分别工作正常,如何将 Instagram 添加到?有可能吗? 问题答案: 我认为将其他社交共享添加到您为Instagram编写的代码中会更加容易。“ .igo”扩展名是Instagram专有的,因此其

  • 问题内容: 我今天在这个主题上搜索了很多。但是我找不到它,如何将JSONArray添加到JSONObject? 因为每次执行此操作都会收到此错误:Stackoverflow 我收到此错误: 我想要的JSON:只有最后一个JsonArray出问题了: ,“ vloer”:[{formaat:’vierkant10x15’tegels:[{naam:’‘,imgThumb:’/bla/bla.png’