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

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

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:

&lt;menu xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
	  xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&gt;
	&lt;item android:id=&quot;@+id/stopCab&quot;
		  android:title=&quot;Stop contextual action bar&quot;
		  app:showAsAction=&quot;ifRoom&quot;
		  android:icon=&quot;@drawable/ic_baseline_cancel_24&quot;/&gt;
	
	&lt;item android:id=&quot;@+id/deleteCab&quot;
		  android:title=&quot;Delete selected items&quot;
		  app:showAsAction=&quot;ifRoom&quot;
		  android:icon=&quot;@drawable/ic_delete_white_24dp&quot;/&gt;
&lt;/menu&gt;

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=&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' 的高度属性:

<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':

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

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

&lt;style name=&quot;ActionModeTheme&quot; parent=&quot;AppTheme&quot;&gt;
	&lt;item name=&quot;windowActionModeOverlay&quot;&gt;true&lt;/item&gt;
	&lt;item name=&quot;actionModeCloseDrawable&quot;&gt;@drawable/ic_delete_white_24dp&lt;/item&gt;
	&lt;item name=&quot;actionMenuTextColor&quot;&gt;@android:color/white&lt;/item&gt;
	&lt;item name=&quot;height&quot;&gt;?attr/actionBarSize&lt;/item&gt;
	&lt;item name=&quot;background&quot;&gt;@color/colorSecondary&lt;/item&gt;
	&lt;item name=&quot;android:src&quot;&gt;@drawable/ic_baseline_close_24&lt;/item&gt;
&lt;/style&gt;

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

&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:

确定