工具栏和上下文动作条与AppCompat-V7 [英] Toolbar and Contextual ActionBar with AppCompat-v7

查看:279
本文介绍了工具栏和上下文动作条与AppCompat-V7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用是在棒棒糖和AppCompat-V7库中引入新添加的工具栏。我跟着关于建立我注意到,当你调用的东西,将弹出上下文动作条,该工具条本指南(如凸显文字的复制/粘贴),它将在工具栏往下推在页面上。你可以看到我的形象在页面底部的念叨着:

I am working on using the newly added Toolbar that was introduced in Lollipop and the AppCompat-v7 library. I followed this guide on setting up the Toolbar I noticed that when you invoke something that will bring up the contextual ActionBar (such as highlighting text for copy/pasting), that it will push the Toolbar down on the page. You can see what I am talking about in the image at the bottom of the page:

因此​​,从本质上讲,我设置了这个样子。我有,我有使用包括标签的XML文件中定义的工具栏:

So, essentially, I set it up like this. I have the Toolbar defined in an xml file that I use with include tags:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"/>

然后,我实例在我的观点:

Then, I instantiate it in my view:

<LinearLayout
    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:orientation="vertical"
    android:id="@+id/root"
    tools:context=".MainActivity">

    <include
        layout="@layout/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/> 

    <!-- Rest of view -->

    </LinearLayout>

在code,我设置它像这样:

In code, I set it up like so:

    // On Create method of activity:
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

有谁知道如何让这个语境动作条来工具栏的上层建筑?

Does anyone know how to make it so that the Contextual ActionBar comes overtop of the Toolbar?

推荐答案

解决方法:使用<一个href="http://developer.android.com/reference/android/R.attr.html#windowActionModeOverlay">windowActionModeOverlay属性。在你的主题设定这样的:

Update:

Solution: use the windowActionModeOverlay property. Set this in your theme:

<item name="windowActionModeOverlay">true</item>

和actionmode将显示在操作栏,而不是推下来进行。 (如果你没有使用最新的AppCompat,那么你需要的机器人添加preFIX的属性)。它基本上可以让AppCompat知道你有位于屏幕顶部的工具栏,它应该利用它上面的ActionMode。

and the actionmode will be shown over the action bar instead of pushing it down. (If you're not using the latest AppCompat then you need to add the "android:" prefix to the property). It basically lets AppCompat know that you have a toolbar located in the top of the screen and that it should draw the ActionMode on top of it.

我遇到了同样的问题。不管我设置什么样的主题,它总是推下来,我设定为动作条工具栏。我试着用和不支持库,但是这并不重要。

I ran into the same problem. No matter what theme I set, it always pushes down the Toolbar I set as ActionBar. I tried with and without the support library, but it didn't matter.

不幸的是我没能解决它,所以我建立了一个解决办法,而不是。在我的 ActionModeCallback onCreateActionMode 我隐藏操作栏:

Unfortunately I was not able to fix it so I have built a workaround instead. In my ActionModeCallback's onCreateActionMode I hide the action bar:

actionBarToolbar.setVisibility(View.GONE);

onDestroyActionMode 我再次显示它:

actionBarToolbar.setVisibility(View.VISIBLE);

在隐藏/显示会发生这么快就没有明显的在我的测试设备。当然有一个缺点:虽然进入动画仍然有效,退出动画的上下文操作栏的丢失,因为工具栏立即弹出了它。但是,直到我们遇到一个更好的解决方案,我想我们被卡住了这一点。

The hiding/showing happens so quickly it is not noticeable on my test devices. There is of course a downside: although the enter-animation still works, the exit-animation of the contextual action bar gets lost because the Toolbar immediately pops over it. But until we come across a better solution I guess we are stuck with this.

(我的活动实际上是延伸自定义的 BaseActivity 里面有一个名为方法类 getActionBarToolbar(),摘自在<一个href="https://github.com/google/iosched/blob/dfaf8b83ad1b3e7c8d1af0b08d59caf4223e0b95/android/src/main/java/com/google/samples/apps/iosched/ui/BaseActivity.java">Google I / O 2014年的应用程序源$ C ​​$ C ,这样我就可以很容易地得到获取工具栏:

(My Activity is actually extending a custom BaseActivity class which has a method called getActionBarToolbar(), taken from the Google I/O 2014 app source code, so I can easily get fetch the Toolbar:

BaseActivity activity = (BaseActivity) getActivity();
activity.getActionBarToolbar().setVisibility(View.GONE);

太糟糕了I / O的应用程序不使用上下文操作栏。)

Too bad the I/O app does not use the contextual action bar.)

这篇关于工具栏和上下文动作条与AppCompat-V7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆