我正在查看使用ActionbarSherlock,但有一个查询使我退缩。

因此,我的应用程序需要完全向后兼容API级别7。

我需要在我的应用程序中实现新的Google Maps,并且需要使用SupportMapFragment类。

** 我的问题 **

是否可以将SupportMapFragment类与ActionBarSherlock一起使用?

提前谢谢

编辑

我已经下载并浏览了图书馆。我可以看到的扩展Fragments的唯一更改非常简单,并且所有这些都相同。

您认为我自己可以增加支持吗?

这是它们的支持方式。

package com.actionbarsherlock.app;

import android.app.Activity;
import android.support.v4.app.DialogFragment;
import com.actionbarsherlock.internal.view.menu.MenuItemWrapper;
import com.actionbarsherlock.internal.view.menu.MenuWrapper;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

import static com.actionbarsherlock.app.SherlockFragmentActivity.OnCreateOptionsMenuListener;
import static com.actionbarsherlock.app.SherlockFragmentActivity.OnOptionsItemSelectedListener;
import static com.actionbarsherlock.app.SherlockFragmentActivity.OnPrepareOptionsMenuListener;

public class SherlockDialogFragment extends DialogFragment implements OnCreateOptionsMenuListener, OnPrepareOptionsMenuListener, OnOptionsItemSelectedListener {
    private SherlockFragmentActivity mActivity;

public SherlockFragmentActivity getSherlockActivity() {
    return mActivity;
}

@Override
public void onAttach(Activity activity) {
    if (!(activity instanceof SherlockFragmentActivity)) {
        throw new IllegalStateException(getClass().getSimpleName() + " must be attached to a SherlockFragmentActivity.");
    }
    mActivity = (SherlockFragmentActivity)activity;

    super.onAttach(activity);
}

@Override
public void onDetach() {
    mActivity = null;
    super.onDetach();
}

@Override
public final void onCreateOptionsMenu(android.view.Menu menu, android.view.MenuInflater inflater) {
    onCreateOptionsMenu(new MenuWrapper(menu), mActivity.getSupportMenuInflater());
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    //Nothing to see here.
}

@Override
public final void onPrepareOptionsMenu(android.view.Menu menu) {
    onPrepareOptionsMenu(new MenuWrapper(menu));
}

@Override
public void onPrepareOptionsMenu(Menu menu) {
    //Nothing to see here.
}

@Override
public final boolean onOptionsItemSelected(android.view.MenuItem item) {
    return onOptionsItemSelected(new MenuItemWrapper(item));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    //Nothing to see here.
    return false;
}

}

最佳答案

这一切都像一个魅力,主要是由于 jack (Jake)在ActionBarSherlock上的出色工作

这是我遵循的步骤:

  • 在actiobarsherlock库项目中创建一个SherlockMapFragment类。我只是简单地制作了一个SherlockFragment副本,因为我还需要对setHasOptionsMenu(true)
  • 的支持
  • 该 Activity 扩展了SherlockFragmentActivity(照常)
  • 该片段扩展了新创建的SherlockMapFragment
  • ActionBarSherlock需要新的google-play-services_lib
  • 您的项目需要ActionBarSherlock库。无需再次包含google-play-services或根据Google API进行构建。
  • 如Graham所述,您必须放弃对API 7设备的支持:<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />

  • 以下是一些代码,可以进行更详细的说明:

    SherlockMapFragment
    package com.actionbarsherlock.app;
    
    import com.actionbarsherlock.internal.view.menu.MenuItemWrapper;
    import com.actionbarsherlock.internal.view.menu.MenuWrapper;
    import com.actionbarsherlock.view.Menu;
    import com.actionbarsherlock.view.MenuInflater;
    import com.actionbarsherlock.view.MenuItem;
    import com.google.android.gms.maps.SupportMapFragment;
    
    import android.app.Activity;
    import android.support.v4.app.Watson.OnCreateOptionsMenuListener;
    import android.support.v4.app.Watson.OnOptionsItemSelectedListener;
    import android.support.v4.app.Watson.OnPrepareOptionsMenuListener;
    
    public class SherlockMapFragment extends SupportMapFragment
            implements OnCreateOptionsMenuListener,
            OnPrepareOptionsMenuListener,
            OnOptionsItemSelectedListener {
        private SherlockFragmentActivity mActivity;
    
        public SherlockFragmentActivity getSherlockActivity() {
            return mActivity;
        }
    
        @Override
        public void onAttach(Activity activity) {
            if (!(activity instanceof SherlockFragmentActivity)) {
                throw new IllegalStateException(getClass().getSimpleName()
                        + " must be attached to a SherlockFragmentActivity.");
            }
            mActivity = (SherlockFragmentActivity) activity;
    
            super.onAttach(activity);
        }
    
        @Override
        public void onDetach() {
            mActivity = null;
            super.onDetach();
        }
    
        @Override
        public final void onCreateOptionsMenu(android.view.Menu menu, android.view.MenuInflater inflater) {
            onCreateOptionsMenu(new MenuWrapper(menu), mActivity.getSupportMenuInflater());
        }
    
        @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            // Nothing to see here.
        }
    
        @Override
        public final void onPrepareOptionsMenu(android.view.Menu menu) {
            onPrepareOptionsMenu(new MenuWrapper(menu));
        }
    
        @Override
        public void onPrepareOptionsMenu(Menu menu) {
            // Nothing to see here.
        }
    
        @Override
        public final boolean onOptionsItemSelected(android.view.MenuItem item) {
            return onOptionsItemSelected(new MenuItemWrapper(item));
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Nothing to see here.
            return false;
        }
    }
    

    Activity :
    public class MainActivity extends SherlockFragmentActivity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    }
    

    Activity XML布局:
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
    
        <fragment
            android:id="@+id/fragment_map"
            android:name=".ui.fragments.MapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="tag_fragment_map" />
    
        <fragment
            android:id="@+id/fragment_help"
            android:name=".ui.fragments.HelpFragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:tag="tag_fragment_help" />
    </FrameLayout>
    

    map 片段:
    public class MapFragment extends SherlockMapFragment {
        private GoogleMap mMap;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View root = super.onCreateView(inflater, container, savedInstanceState);
            mMap = getMap();
            return root;
        }
    }
    

    关于android - 将ActionBarSherlock与新的SupportMapFragment一起使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13721929/

    10-12 05:49