问题
Problem
First my ActionBar (with custom menu) is purple, then:
- Trigger CAB by long-click on my ListView
- Change phone orientation
- Cancel CAB
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?
UPDATE: Tried on API 25 emulator, and this does not happen.
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:
<?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:
<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:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.switch_menu, menu);
//...stuff...
return true;
}
setting up CAB in onCreate()
:
historyListView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
historyListView.setMultiChoiceModeListener(choiceModeListener);
inside 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
:
<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>
回答1:
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.
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. 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.
来源:https://stackoverflow.com/questions/48688177/actionbar-changes-colour-after-changing-phone-orientation-and-closing-contextual