简体   繁体   English

在更改电话方向并关闭上下文操作栏后,ActionBar会更改颜色

[英]ActionBar changes colour after changing phone orientation and closing Contextual Action Bar

Problem 问题

First my ActionBar (with custom menu) is purple, then: 首先,我的ActionBar(带有自定义菜单)是紫色,然后:

  1. Trigger CAB by long-click on my ListView 通过长按我的ListView来触发CAB
  2. Change phone orientation 更改手机方向
  3. Cancel CAB 取消CAB

ActionBar becomes white. ActionBar变为白色。 This does not happen without phone orientation change. 如果不更改手机方向,则不会发生这种情况。 I have tried this on android 4.4.4 , minSdkVersion="19" , targetSdkVersion="26" . 我已经在android 4.4.4上尝试过了, minSdkVersion="19"targetSdkVersion="26" Can you please advise me, why this could happen? 您能告诉我为什么会发生这种情况吗?

UPDATE: Tried on API 25 emulator, and this does not happen. 更新:尝试在API 25仿真器上,并且不会发生。


Sources 资料来源

Activity layout: 活动布局:

<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 - classic Action Bar: 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 - Contextual Action Bar: 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>

initializing ActionBar in activity: 在活动中初始化ActionBar:

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

    return true;
}

setting up CAB in onCreate() : onCreate()设置CAB:

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

inside MultiChoiceModeListener : 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;
}

in <application> inside Manifest this style is set android:theme="@style/AppTheme" from styles.xml : 在清单中的<application> ,此样式是从styles.xml设置的android:theme="@style/AppTheme"

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

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. 在我的情况下, ActionBar的支持会变得更暗,两边都带有小“阴影”。

The following hack works on all API versions: 以下技巧适用于所有API版本:

@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;
}

PS Basically, you just have to perform any simple animation on the ActionBar view. PS基本上,您只需要在ActionBar视图上执行任何简单的动画即可。 If you find the root of the problem in the source code, or maybe more elegant way to fix it, please let me know. 如果您在源代码中找到问题的根源,或者找到了解决问题的更优雅的方法,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM