Android 带有 AppCompat-v7 的工具栏和上下文操作栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26443403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Toolbar and Contextual ActionBar with AppCompat-v7
提问by ariets
I am working on using the newly added Toolbar that was introduced in Lollipop and the AppCompat-v7 library. I followed this guideon 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:
我正在使用 Lollipop 和 AppCompat-v7 库中引入的新添加的工具栏。我按照本指南设置工具栏我注意到,当您调用一些会显示上下文操作栏的内容(例如突出显示文本以进行复制/粘贴)时,它会将工具栏向下推到页面上。你可以在页面底部的图片中看到我在说什么:
So, essentially, I set it up like this. I have the Toolbar defined in an xml file that I use with include tags:
所以,基本上,我是这样设置的。我在与包含标签一起使用的 xml 文件中定义了工具栏:
<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>
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?
有谁知道如何使上下文操作栏位于工具栏的上方?
回答by Jacob Ras
Update:
更新:
Solution: use the windowActionModeOverlayproperty. Set this in your theme:
解决方案:使用windowActionModeOverlay属性。在您的主题中设置:
<item name="windowActionModeOverlay">true</item>
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.
并且动作模式将显示在动作栏上,而不是将其按下。(如果您没有使用最新的 AppCompat,则需要在属性中添加“android:”前缀)。它基本上让 AppCompat 知道你有一个位于屏幕顶部的工具栏,它应该在它上面绘制 ActionMode。
Old answer/workaround:
旧答案/解决方法:
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.
我遇到了同样的问题。无论我设置什么主题,它总是将我设置为 ActionBar 的 Toolbar 下推。我尝试过使用和不使用支持库,但这并不重要。
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:
不幸的是我无法修复它,所以我建立了一个解决方法。在 my 中ActionModeCallback
,onCreateActionMode
我隐藏了操作栏:
actionBarToolbar.setVisibility(View.GONE);
and in onDestroyActionMode
I show it again:
在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.
隐藏/显示发生得如此之快,在我的测试设备上并不明显。当然有一个缺点:虽然进入动画仍然有效,但是上下文操作栏的退出动画会丢失,因为工具栏会立即弹出它。但是,在我们遇到更好的解决方案之前,我想我们会坚持这一点。
(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:
(我的 Activity 实际上是扩展一个自定义BaseActivity
类,它有一个名为 的方法getActionBarToolbar()
,取自Google I/O 2014 应用程序源代码,因此我可以轻松获取工具栏:
BaseActivity activity = (BaseActivity) getActivity();
activity.getActionBarToolbar().setVisibility(View.GONE);
Too bad the I/O app does not use the contextual action bar.)
太糟糕了 I/O 应用程序不使用上下文操作栏。)
回答by Frank
Do not start it on your activity, but on your toolbar. In you activity:
不要在您的活动中启动它,而是在您的工具栏上启动它。在你的活动中:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.startActionMode(mActionModeCallback)
and you have to use
你必须使用
<item name="windowActionModeOverlay">true</item>
回答by Daniel Veihelmann
Just a small addition: For
只是一个小小的补充:对于
<item name="windowActionModeOverlay">true</item>
工作,打电话很重要 super.onCreate(savedInstanceState)
super.onCreate(savedInstanceState)
BEFORE在调用setContentView(R.layout.your_activity)
setContentView(R.layout.your_activity)
您的活动之前。在这种情况下,这真的很重要!回答by Jenkyn
In my case,
<item name="windowActionModeOverlay">true</item>
did not work,
but this work:<item name="android:windowActionModeOverlay">true</item>
,the android
is the key.
在我的情况下,
<item name="windowActionModeOverlay">true</item>
没有工作,但这项工作:<item name="android:windowActionModeOverlay">true</item>
这android
是关键。
回答by Pete
Jacob's solution worked for me but the contextual ActionBar was transparent and the Toolbar visible through it. This can be resolved as follows:
Jacob 的解决方案对我有用,但上下文 ActionBar 是透明的,并且工具栏通过它可见。这可以解决如下:
<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
....
....
<item name="actionModeStyle">@style/CustomActionMode</item>
</style>
<style name="CustomActionMode" parent="@style/Widget.AppCompat.ActionMode">
<item name="background">@color/primary_material_light</item>
</style>
The theme "AppTheme.Base" must be the one applied to the Toolbar.
主题“AppTheme.Base”必须是应用于工具栏的主题。
More details regarding contextual ActionBar styling:
有关上下文 ActionBar 样式的更多详细信息:
how to Customize the Contextual Action Bar using appCompat in material design
回答by Andreas Wenger
Another small addition: make sure to set at least an empty screen in the activity via setContentView(R.layout.empty_screen)
if you load the whole ui in fragments (ft.replace(android.R.id.content, fragment)
).
另一个小补充:setContentView(R.layout.empty_screen)
如果您在片段 ( ft.replace(android.R.id.content, fragment)
) 中加载整个 ui,请确保在 Activity 中至少设置一个空屏幕。
回答by kunal.c
Very useful method to bring toolbar to front toolbar.bringToFront()
将工具栏置于最前面的非常有用的方法 toolbar.bringToFront()