行动模式栏未覆盖整个标题栏。

huangapple go评论99阅读模式
英文:

Actionmode Bar doesn't cover whole titlebar

问题

在我的应用程序中,我需要使用 ActionMode。

我已经按照几个指南和教程(例如:https://medium.com/asos-techblog/style-actionmode-on-android-5e613fa77c32,https://developer.android.com/guide/topics/ui/menus)来实现我的上下文操作菜单,但是正如您在这张图片中所见:

图片链接

标题栏的底部仍然可见一部分。我的问题是,是否有任何方法可以改变这种行为。

我还尝试过更改 ActionMode 样式,但是当我更改样式时,它变得不可见(只显示图标),并且标题栏完全可见。

我对 ActionMode 除了包含按钮的菜单进行了任何更改:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <menu xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto">
  4. <item android:id="@+id/stopCab"
  5. android:title="Stop contextual action bar"
  6. app:showAsAction="ifRoom"
  7. android:icon="@drawable/ic_baseline_cancel_24"/>
  8. <item android:id="@+id/deleteCab"
  9. android:title="Delete selected items"
  10. app:showAsAction="ifRoom"
  11. android:icon="@drawable/ic_delete_white_24dp"/>
  12. </menu>

初始化 ActionMode:

  1. private ActionMode.Callback actionModeCallback = new ActionMode.Callback() {
  2. @Override
  3. public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
  4. MenuInflater menuInflater = getMenuInflater();
  5. menuInflater.inflate(R.menu.contextual_action_bar_menu, menu);
  6. return true;
  7. }
  8. @Override
  9. public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
  10. return false;
  11. }
  12. @Override
  13. public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
  14. switch (menuItem.getItemId()) {
  15. case R.id.deleteCab:
  16. //TODO: do something
  17. return true;
  18. case R.id.stopCab:
  19. //TODO: do something
  20. return true;
  21. default:
  22. return false;
  23. }
  24. }
  25. @Override
  26. public void onDestroyActionMode(ActionMode actionMode) {
  27. FieldChooserActivity.this.actionMode = null;
  28. }
  29. };

启动 ActionMode:

  1. FieldChooserActivity.this.actionMode = startActionMode(FieldChooserActivity.this.actionModeCallback);

在我的样式中,AppTheme 的父样式是 Theme.AppCompat.Light.DarkActionBar,每个 Activity 中都有:

  1. android:theme="@style/AppTheme.NoActionBar">

在 AndroidManifest 中。也许这就是我无法更改样式而不使 ActionMode 变得不可见的原因。

我希望有人能够帮助我解决这个问题,提前感谢!

英文:

In my app I need to use the ActionMode.

I have followed several Guides and Tutorials (https://medium.com/asos-techblog/style-actionmode-on-android-5e613fa77c32, https://developer.android.com/guide/topics/ui/menus e.g.) on implementing my Contextual Action Menu but as you can see in this image:

https://i.stack.imgur.com/ag2Mp.jpg

There is part of the titleBar still visible on the bottom. My question is if there is anything i can do to change this behaviour.

I also tried to change the style of my ActionModeBar but when i change the styling it becomes invisible (only showing the icons) and the titlebar is completely visible.

I have not made any changes to the ActionMode except the menu that contains the buttons:

  1. &lt;menu xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  2. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&gt;
  3. &lt;item android:id=&quot;@+id/stopCab&quot;
  4. android:title=&quot;Stop contextual action bar&quot;
  5. app:showAsAction=&quot;ifRoom&quot;
  6. android:icon=&quot;@drawable/ic_baseline_cancel_24&quot;/&gt;
  7. &lt;item android:id=&quot;@+id/deleteCab&quot;
  8. android:title=&quot;Delete selected items&quot;
  9. app:showAsAction=&quot;ifRoom&quot;
  10. android:icon=&quot;@drawable/ic_delete_white_24dp&quot;/&gt;
  11. &lt;/menu&gt;

Initializing the ActionMode:

  1. private ActionMode.Callback actionModeCallback = new ActionMode.Callback() {
  2. @Override
  3. public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
  4. MenuInflater menuInflater = getMenuInflater();
  5. menuInflater.inflate(R.menu.contextual_action_bar_menu, menu);
  6. return true;
  7. }
  8. @Override
  9. public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) {
  10. return false;
  11. }
  12. @Override
  13. public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
  14. switch (menuItem.getItemId()) {
  15. case R.id.deleteCab:
  16. //TODO: do something
  17. return true;
  18. case R.id.stopCab:
  19. //TODO: do something
  20. return true;
  21. default:
  22. return false;
  23. }
  24. }
  25. @Override
  26. public void onDestroyActionMode(ActionMode actionMode) {
  27. FieldChooserActivity.this.actionMode = null;
  28. }
  29. };

And starting the ActionMode:

  1. FieldChooserActivity.this.actionMode = startActionMode(FieldChooserActivity.this.actionModeCallback);

In my Style the AppTheme parent is Theme.AppCompat.Light.DarkActionBar
And each Activity has

  1. android:theme=&quot;@style/AppTheme.NoActionBar&quot;&gt;

in AndroidManifest.
Maybe this is the fault why i cant change the style without the ActionMode becoming invisible.

I hope someone can help me with this matter and thanks in advance!

答案1

得分: 0

我找到了一个解决方案,解决了我的问题(即 ActionMode 栏不覆盖工具栏的问题)。

当我查看我的活动布局时,我注意到在工具栏中有一个名为 '?attr/actionBarSize' 的高度属性:

  1. <androidx.appcompat.widget.Toolbar
  2. android:id="@+id/fieldChooserToolbar"
  3. android:layout_width="match_parent"
  4. android:layout_height="?attr/actionBarSize"
  5. android:background="?attr/colorPrimary"
  6. app:popupTheme="@style/AppTheme.PopupOverlay">

然后,我将这个高度添加到我自定义的 ActionMode 样式中:

  1. <style name="ActionModeTheme" parent="AppTheme">
  2. <item name="windowActionModeOverlay">true</item>
  3. <item name="actionModeCloseDrawable">@drawable/ic_delete_white_24dp</item>
  4. <item name="actionMenuTextColor">@android:color/white</item>
  5. <item name="height">?attr/actionBarSize</item>
  6. <item name="background">@color/colorSecondary</item>
  7. <item name="android:src">@drawable/ic_baseline_close_24</item>
  8. </style>

最后一部分是我将以下行添加到了我的 AppTheme 样式中:

  1. <item name="actionModeStyle">@style/ActionModeTheme</item>

然后问题解决了!

第二个问题,即我的 ActionMode 栏不可见的问题,很可能是因为将 actionMode 样式添加到了 AppTheme.NoActionBar 中。

英文:

I found a solution that fixed my problem (where the ActionMode bar doesn't cover the Toolbar).

As i looked at the layout of my activity i saw that in the Toolbar is a height attribute called '?attr/actionBarSize':

  1. &lt;androidx.appcompat.widget.Toolbar
  2. android:id=&quot;@+id/fieldChooserToolbar&quot;
  3. android:layout_width=&quot;match_parent&quot;
  4. android:layout_height=&quot;?attr/actionBarSize&quot;
  5. android:background=&quot;?attr/colorPrimary&quot;
  6. app:popupTheme=&quot;@style/AppTheme.PopupOverlay&quot;&gt;

I then added the height to a custom style for my ActionMode:

  1. &lt;style name=&quot;ActionModeTheme&quot; parent=&quot;AppTheme&quot;&gt;
  2. &lt;item name=&quot;windowActionModeOverlay&quot;&gt;true&lt;/item&gt;
  3. &lt;item name=&quot;actionModeCloseDrawable&quot;&gt;@drawable/ic_delete_white_24dp&lt;/item&gt;
  4. &lt;item name=&quot;actionMenuTextColor&quot;&gt;@android:color/white&lt;/item&gt;
  5. &lt;item name=&quot;height&quot;&gt;?attr/actionBarSize&lt;/item&gt;
  6. &lt;item name=&quot;background&quot;&gt;@color/colorSecondary&lt;/item&gt;
  7. &lt;item name=&quot;android:src&quot;&gt;@drawable/ic_baseline_close_24&lt;/item&gt;
  8. &lt;/style&gt;

The last part was that i added following line to my AppTheme-styling:

  1. &lt;item name=&quot;actionModeStyle&quot;&gt;@style/ActionModeTheme&lt;/item&gt;

And then it worked!

The second problem where my ActionMode bar was invisible was most likely caused by adding the actionModeStyling to the AppTheme.NoActionBar.

huangapple
  • 本文由 发表于 2020年8月28日 16:09:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63629884.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定