英文:
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 除了包含按钮的菜单进行了任何更改:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/stopCab"
android:title="Stop contextual action bar"
app:showAsAction="ifRoom"
android:icon="@drawable/ic_baseline_cancel_24"/>
<item android:id="@+id/deleteCab"
android:title="Delete selected items"
app:showAsAction="ifRoom"
android:icon="@drawable/ic_delete_white_24dp"/>
</menu>
初始化 ActionMode:
private ActionMode.Callback actionModeCallback = new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.contextual_action_bar_menu, menu);
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.deleteCab:
//TODO: do something
return true;
case R.id.stopCab:
//TODO: do something
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode actionMode) {
FieldChooserActivity.this.actionMode = null;
}
};
启动 ActionMode:
FieldChooserActivity.this.actionMode = startActionMode(FieldChooserActivity.this.actionModeCallback);
在我的样式中,AppTheme 的父样式是 Theme.AppCompat.Light.DarkActionBar,每个 Activity 中都有:
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:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/stopCab"
android:title="Stop contextual action bar"
app:showAsAction="ifRoom"
android:icon="@drawable/ic_baseline_cancel_24"/>
<item android:id="@+id/deleteCab"
android:title="Delete selected items"
app:showAsAction="ifRoom"
android:icon="@drawable/ic_delete_white_24dp"/>
</menu>
Initializing the ActionMode:
private ActionMode.Callback actionModeCallback = new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.contextual_action_bar_menu, menu);
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.deleteCab:
//TODO: do something
return true;
case R.id.stopCab:
//TODO: do something
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode actionMode) {
FieldChooserActivity.this.actionMode = null;
}
};
And starting the ActionMode:
FieldChooserActivity.this.actionMode = startActionMode(FieldChooserActivity.this.actionModeCallback);
In my Style the AppTheme parent is Theme.AppCompat.Light.DarkActionBar
And each Activity has
android:theme="@style/AppTheme.NoActionBar">
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' 的高度属性:
<androidx.appcompat.widget.Toolbar
android:id="@+id/fieldChooserToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
然后,我将这个高度添加到我自定义的 ActionMode 样式中:
<style name="ActionModeTheme" parent="AppTheme">
<item name="windowActionModeOverlay">true</item>
<item name="actionModeCloseDrawable">@drawable/ic_delete_white_24dp</item>
<item name="actionMenuTextColor">@android:color/white</item>
<item name="height">?attr/actionBarSize</item>
<item name="background">@color/colorSecondary</item>
<item name="android:src">@drawable/ic_baseline_close_24</item>
</style>
最后一部分是我将以下行添加到了我的 AppTheme 样式中:
<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':
<androidx.appcompat.widget.Toolbar
android:id="@+id/fieldChooserToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
I then added the height to a custom style for my ActionMode:
<style name="ActionModeTheme" parent="AppTheme">
<item name="windowActionModeOverlay">true</item>
<item name="actionModeCloseDrawable">@drawable/ic_delete_white_24dp</item>
<item name="actionMenuTextColor">@android:color/white</item>
<item name="height">?attr/actionBarSize</item>
<item name="background">@color/colorSecondary</item>
<item name="android:src">@drawable/ic_baseline_close_24</item>
</style>
The last part was that i added following line to my AppTheme-styling:
<item name="actionModeStyle">@style/ActionModeTheme</item>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论