5

我想将 ActionBarSherlock 与 Google 地图 API V2 一起使用。我在这里阅读了一个解决方案。但是当扩展我的主要 ActionBarSherlock 类时,它缺少很多用于 menthod 的库(例如:findViewById、getSupportActionBar ..)

这是我的 ActionBarSherlock

public class FeatureToggles extends SherlockMapFragment  {
    private static final Random RANDOM  = new Random();
    private int                         items       = 0;

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        for(int i = 0; i < items; i++) {
            menu.add("Text").setIcon(R.drawable.ic_title_share_default).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        setTheme(R.style.Theme_Sherlock); // Used for theme switching in samples
        requestWindowFeature(Window.FEATURE_PROGRESS);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.feature_toggles);
        setSupportProgressBarIndeterminateVisibility(false);
        setSupportProgressBarVisibility(false);
        getSupportActionBar().setCustomView(R.layout.custom_view);
        getSupportActionBar().setDisplayShowCustomEnabled(false);
        Context context = getSupportActionBar().getThemedContext();
        ArrayAdapter<CharSequence> listAdapter = ArrayAdapter.createFromResource(context, R.array.locations, R.layout.sherlock_spinner_item);
        listAdapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
        getSupportActionBar().setListNavigationCallbacks(listAdapter, null);



//      if (savedInstanceState == null) {
//          // Do first time initialization -- add initial fragment.
//          Fragment newFragment = CountingFragment.newInstance(mStackLevel);
//          FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
//          ft.add(R.id.simple_fragment, newFragment).commit();
//      } else {
//          mStackLevel = savedInstanceState.getInt("level");
//      }   
//      
//      findViewById(R.id.new_fragment).setOnClickListener(new View.OnClickListener() {
//          public void onClick(View v){
//              addFragmentToStack();
//          }
//      });     
        findViewById(R.id.btnMap).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v){
                Intent i = new Intent(getApplicationContext(), GoogleActivityV2.class);
                startActivity(i);
            }
        });     
        findViewById(R.id.display_progress_show).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v){
                setSupportProgressBarVisibility(true);
                setSupportProgressBarIndeterminateVisibility(false);
                setSupportProgress(RANDOM.nextInt(8000) + 10);
            }
        });
        findViewById(R.id.display_progress_hide).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v){
                setSupportProgressBarVisibility(false);
            }
        });
        findViewById(R.id.display_iprogress_show).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v){
                // Hack to hide the regular progress bar
                setSupportProgress(Window.PROGRESS_END);
                setSupportProgressBarIndeterminateVisibility(true);
            }
        });
        findViewById(R.id.display_iprogress_hide).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v){
                setSupportProgressBarIndeterminateVisibility(false);
            }
        });
        findViewById(R.id.display_items_clear).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v){
                items = 0;
                invalidateOptionsMenu();
            }
        });
        findViewById(R.id.display_items_add).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v){
                items += 1;
                invalidateOptionsMenu();
            }
        });
        findViewById(R.id.display_subtitle_show).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){
                getSupportActionBar().setSubtitle("The quick brown fox jumps over the lazy dog.");
            }
        });
        findViewById(R.id.display_subtitle_hide).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){
                getSupportActionBar().setSubtitle(null);
            }
        });
        findViewById(R.id.display_title_show).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){
                getSupportActionBar().setDisplayShowTitleEnabled(true);
            }
        });
        findViewById(R.id.display_title_hide).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){
                getSupportActionBar().setDisplayShowTitleEnabled(false);
            }
        });
        findViewById(R.id.display_custom_show).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){
                getSupportActionBar().setDisplayShowCustomEnabled(true);
            }
        });
        findViewById(R.id.display_custom_hide).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){
                getSupportActionBar().setDisplayShowCustomEnabled(false);
            }
        });
        findViewById(R.id.navigation_standard).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){
                getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
            }
        });
        findViewById(R.id.navigation_list).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){
                getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
            }
        });
        findViewById(R.id.navigation_tabs).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){
                getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            }
        });
        findViewById(R.id.display_home_as_up_show).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            }
        });
        findViewById(R.id.display_home_as_up_hide).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){
                getSupportActionBar().setDisplayHomeAsUpEnabled(false);
            }
        });
        findViewById(R.id.display_logo_show).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){
                getSupportActionBar().setDisplayUseLogoEnabled(true);
            }
        });
        findViewById(R.id.display_logo_hide).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){
                getSupportActionBar().setDisplayUseLogoEnabled(false);
            }
        });
        findViewById(R.id.display_home_show).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){
                getSupportActionBar().setDisplayShowHomeEnabled(true);
            }
        });
        findViewById(R.id.display_home_hide).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){
                getSupportActionBar().setDisplayShowHomeEnabled(false);
            }
        });
        findViewById(R.id.display_actionbar_show).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){
                getSupportActionBar().show();
            }
        });
        findViewById(R.id.display_actionbar_hide).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view){
                getSupportActionBar().hide();
            }
        });


    }

    public void onTabSelected(Tab tab, FragmentTransaction transaction){
    }

    public void onTabUnselected(Tab tab, FragmentTransaction transaction){
    }

    public void onTabReselected(Tab tab, FragmentTransaction transaction){
    }
}
4

1 回答 1

3

这是我使用全屏地图的方法(请注意,在您的 ABS 选项卡内的片段中使用 GoogleMap API V2 会在您滑动时添加一些令人敬畏的黑边,这是一个 API 问题,所以......):

地图片段的布局:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.package.TransparentSupportMapFragment" />

地图的一个类(这可能有助于做一些事情,比如更正 API 错误..)

public class TransparentSupportMapFragment extends SupportMapFragment {
    public TransparentSupportMapFragment() {

        super();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup view, Bundle savedInstance) {

        View layout = super.onCreateView(inflater, view, savedInstance);
        FrameLayout frameLayout = new FrameLayout(getActivity());
        frameLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent));
        ((ViewGroup) layout).addView(frameLayout, new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        return layout;
    }
}

一个 FragmentActivity(或 SherlockFragmentActivity)

public class ActivityGoogleMap extends SherlockFragmentActivity{

    GoogleMap _googleMap;

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        // abs
        ActionBar bar = getSupportActionBar();
        bar.setTitle(R.string.tle_abs);
        bar.setDisplayHomeAsUpEnabled(true);

        // map
        _googleMap = ((TransparentSupportMapFragment) getSupportFragmentManager().findFragmentById((R.id.map))).getMap();
}

没有动画的简单过渡:

Intent intent = new Intent(this, ActivityGoogleMap.class);
startActivity(intent);

现在进行清单工作:

OpenGLES2:

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

一些权限:

     <permission
        android:name="com.egdigital.appetablissement.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />
 <uses-permission android:name="android.permission.INTERNET" />
<!-- GPS LOCATION -->
    <uses-permission android:name="android.permission.ACCESS_GPS" />
    <uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

您在 Application 中的 API 密钥:

<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="HERE_THE_KEY" />

您的地图活动:

<activity
    android:name=".ActivityGoogleMap"
    android:configChanges="orientation"
    android:label="@string/app_name" >
</activity>

希望它会帮助好运

于 2013-07-09T15:41:29.517 回答