工具栏顶部的上下文叠加堆叠

时间:2015-09-12 04:34:11

标签: android android-fragments navigation-drawer material-design android-toolbar

我已经阅读了关于此的每一个答案,但似乎仍然无法使其发挥作用。当我拨打startSupportActionMode时,操作模式菜单堆叠在工具栏顶部而不是替换它。此图形来自另一个具有相同问题的用户。

enter image description here

通过在样式中使用windowActionModeOverlay解决了大多数其他问题。这对我来说似乎不起作用。我还确定我要导入修正动作模式。

(import android.support.v7.view.ActionMode;
)

我的情况有点不同。我的主要活动是AppCompatActivity。我根据导航抽屉的选择加载不同的片段。操作栏将根据所选的导航项进行更改。

在我转到appCompat-v7之前,此代码全部有效。我正在尝试转换为新的材料设计,因此用工具栏替换基本操作栏。以下是一些代码段:

清单:

    <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:name="com.acme.common.MINApplication"
    android:largeHeap="true">

    <activity android:name="com.acme.common.MINMainActivity"
              android:theme="@style/AppTheme"
              android:configChanges="orientation|screenSize|keyboardHidden" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

样式:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Main theme colors -->
    <!--   your app branding color for the app bar -->
    <item name="android:colorPrimary">@color/ColorPrimary</item>
    <!-- Support library compatibility -->
    <item name="colorPrimary">@color/ColorPrimary</item>

    <!--   darker variant for the status bar and contextual app bars -->
    <item name="android:colorPrimaryDark">@color/ColorPrimaryDark</item>
    <!-- Support library compatibility -->
    <item name="colorPrimaryDark">@color/ColorPrimaryDark</item>

    <!--   theme UI controls like checkboxes and text fields -->
    <item name="android:statusBarColor">@android:color/transparent</item>

    <item name="windowActionModeOverlay">true</item>
    <item name="android:windowActionModeOverlay">true</item>
</style>

主要活动:

 public class MINMainActivity extends AppCompatActivity 
 {
private final String TAG = ((Object) this).getClass().getSimpleName();

private View mMainView;

// New Toolbar and Navigation View
public Toolbar toolbar;
public RecyclerView recyclerView;
private MINPageAdapter adapter;
private RecyclerView.LayoutManager layoutManager;
public DrawerLayout drawerLayout;
public android.support.v7.app.ActionBarDrawerToggle drawerToggle;

volatile public boolean isDrawerOpen = false;
private Menu mMenu;


@Override
public void onCreate(Bundle savedInstanceState)
{
    supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS | Window.FEATURE_ACTION_BAR_OVERLAY);

    super.onCreate(savedInstanceState);

    // Inflate main view
    mMainView = getLayoutInflater().inflate(R.layout.material_design_drawer_layout, null);

    setContentView(mMainView);

    sharedInstance = this;

    // Setup New Toolbar implementation
    setupToolbar();

    // Setup Recycler Adapter
    setupAdapter();

    // Setup Navigation View
    initNavigationDrawer();

    // Show startup screen/fragment
    showStartupScreen();

    // Color components based on json settings
    setCustomAttributes();

  init();
}

片段:

 public class MINPageTypeGridFragment extends Fragment 
 {
 @Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setRetainInstance(true);
    setHasOptionsMenu(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    // Inflate view
    gridView = (StickyGridHeadersGridView)inflater.inflate(R.layout.page_gridview_fragment, container, false);

    // Set up specific controls/views
    initializeGridView(inflater, container);

    ...

    return gridView;
}

@Override
public void onResume()
{
    ActionBar actionBar = MINMainActivity.getSharedInstance().getSupportActionBar();
    if(actionBar != null)
    {
        actionBar.show();
        actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP |
                                    ActionBar.DISPLAY_SHOW_HOME |
                                    ActionBar.DISPLAY_USE_LOGO |
                                    ActionBar.DISPLAY_SHOW_TITLE);
        actionBar.setTitle(pageDefinition.pageName);
    }

    ...

    super.onResume();
}


@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    int count = 0;

    ...
                freezeGridView();
                mActionMode = MINMainActivity.getSharedInstance().startSupportActionMode(mActionModeCallback);
    ...
    return super.onOptionsItemSelected(item);
}


public ActionMode.Callback mActionModeCallback = new ActionMode.Callback()
{
    @Override
    public boolean onCreateActionMode(ActionMode actionMode, Menu menu)
    {
        MenuItem shareMenuItem;

        ...
                actionMode.getMenuInflater().inflate(R.menu.gridview_edit_menu, menu);
                shareMenuItem = menu.findItem(R.id.MenuItemShare);
                mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareMenuItem);
                if(mShareActionProvider != null)
                {
                    mShareActionProvider.setShareIntent(Share(null));
                    mShareActionProvider.setOnShareTargetSelectedListener(new ShareActionProvider.OnShareTargetSelectedListener()
                    {
                        @Override
                        public boolean onShareTargetSelected(ShareActionProvider shareActionProvider, Intent intent)
                        {
                            if(mActionMode != null)
                            {
                                currentMode = MODE_STANDARD;
                                clearSelectedItems();
                                mActionMode.finish();
                                mActionMode = null;
                            }
                            return false;
                        }
                    });
        ...
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode actionMode, Menu menu)
    {
        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem)
    {
        switch(menuItem.getItemId())
        {
            case R.id.MenuItemEdit:
                launchAlbumItemDetails(MINPageTypeGridFragment.this, currentAlbumItem, pageDefinition.pageConfigFileName);
                return true;
            case R.id.MenuItemDelete:
                deleteItem(MINPageTypeGridFragment.this, currentAlbum, currentAlbumItem);
                return true;
            default:
                return false;
        }
    }

    @Override
    public void onDestroyActionMode(ActionMode actionMode)
    {
        clearSelectedItems();
    }

}

我确信我错过了一些显而易见的事情但我没有想法。

2 个答案:

答案 0 :(得分:1)

你应该删除'重复'定义:android:windowActionModeOverlay - &gt; windowActionModeOverlay; android:colorPrimary - &gt; colorPrimary等。这是一个应该有效的例子:

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="windowActionModeOverlay">true</item>
    <item name="colorControlNormal">@color/appColorControlNormal</item>
    <item name="colorControlHighlight">@color/appColorControlHighlight</item>
    <item name="colorControlActivated">@color/appColorControlActivated</item>
    <item name="colorPrimary">@color/appColorPrimary</item>
    <item name="colorPrimaryDark">@color/appColorPrimaryDark</item>
    <item name="colorAccent">@color/appColorAccent</item>
</style>

答案 1 :(得分:0)

务必将工具栏设置为当前操作栏(也许您的方法setupToolbar已经执行此操作):

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);    
setSupportActionBar(toolbar);

之后,更改代码并直接从工具栏引用开始操作模式:

toolbar.startActionMode(mActionModeCallback)

如果这可以解决您的问题,请告诉我们。