在更改电话方向并关闭上下文操作栏后

在更改电话方向并关闭上下文操作栏后

本文介绍了在更改电话方向并关闭上下文操作栏后,ActionBar会更改颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我的ActionBar(带有自定义菜单)是紫色,然后:

First my ActionBar (with custom menu) is purple, then:

  1. 长按我的ListView触发CAB
  2. 更改手机方向
  3. 取消CAB

ActionBar变为白色.如果不更改手机方向,则不会发生这种情况.我已经在android 4.4.4minSdkVersion="19"targetSdkVersion="26"上尝试过此操作.您能告诉我为什么会发生这种情况吗?

ActionBar becomes white. This does not happen without phone orientation change. I have tried this on android 4.4.4, minSdkVersion="19", targetSdkVersion="26". Can you please advise me, why this could happen?

更新:已在API 25仿真器上进行了尝试,但这不会发生.

UPDATE: Tried on API 25 emulator, and this does not happen.

活动布局:

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="sk.tuke.smart.makac.activities.FreeShootingHistoryActivity">

    <ListView
        android:id="@+id/listview_freeshootinghistory"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:listitem="@android:layout/simple_list_item_activated_2" />
</android.support.constraint.ConstraintLayout>

switch_menu.xml-经典操作栏:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/item_switchmenu_label"
        android:title="@string/switchmenu_colouring"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/item_switchmenu_switch"
        android:title="@string/switchmenu_colouring"
        app:actionViewClass="android.support.v7.widget.SwitchCompat"
        app:showAsAction="always" />
</menu>

delete_menu.xml-上下文操作栏:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/item_deletemenu_delete"
        android:icon="@drawable/ic_action_delete"
        android:title="@string/deletemenu_delete" />
</menu>

在活动中初始化ActionBar:

initializing ActionBar in activity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.switch_menu, menu);
    //...stuff...

    return true;
}

onCreate()中设置CAB:

historyListView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
historyListView.setMultiChoiceModeListener(choiceModeListener);

内部MultiChoiceModeListener:

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    // Inflate the menu for the CAB
    MenuInflater inflater = mode.getMenuInflater();
    inflater.inflate(R.menu.delete_menu, menu);

    //...stuff...

    return true;
}

在清单中的<application>中的

中,此样式是从styles.xml设置为android:theme="@style/AppTheme"的:

in <application> inside Manifest this style is set android:theme="@style/AppTheme" from styles.xml:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">#8600b3</item>
        <item name="colorPrimaryDark">#730099</item>
        <item name="colorAccent">@color/colorPrimaryDark</item>
        <item name="android:colorBackground">#bfc6f6</item>
        <item name="android:textColorSecondary">@color/text_color_secondary</item>
    </style>

</resources>

推荐答案

Lollipop上存在相同的问题,但结果略有不同.在我的情况下,支撑物ActionBar变得更暗,两边都带有小的阴影".

The same issue exists on Lollipop with a slightly different result. In my case the support ActionBar just gets darker with little 'shadows' on both sides.

以下hack适用于所有API版本:

The following hack works on all API versions:

@Override
public void onDestroyActionMode(ActionMode mode) {
    // Find the ActionBar view
    final View view = findActionBarView(
            mActivity.getWindow().getDecorView());

    if (view != null) {
        view.post(new Runnable() {
            @Override
            public void run() {
                // Start view animation
                view.animate().alpha(0f).setDuration(100);
            }
        });
    }
}

public static ViewGroup findActionBarView(View view) {
    try {
        if (view instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view;

            // You can search for the Toolbar if necessary
            if (viewGroup instanceof android.support.v7.widget.ActionBarContainer) {
                for (int i = 0; i < viewGroup.getChildCount(); i++) {
                    View actionView = viewGroup.getChildAt(i);

                    // Return currently visible context bar
                    if (actionView.getVisibility() == View.VISIBLE) {
                        return (ViewGroup) actionView;
                    }
                }
            }

            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                ViewGroup actionView =
                        getActionBarView(viewGroup.getChildAt(i));

                if (actionView != null) {
                    return actionView;
                }
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "View not found", e);
    }

    return null;
}

P.S.基本上,您只需要在ActionBar视图上执行任何简单的动画即可.如果您在源代码中找到问题的根源,或者找到了解决问题的更优雅的方法,请告诉我.

P.S. Basically, you just have to perform any simple animation on the ActionBar view. If you find the root of the problem in the source code, or maybe more elegant way to fix it, please let me know.

这篇关于在更改电话方向并关闭上下文操作栏后,ActionBar会更改颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:21