[英]Why is Contextual Action Bar getting different style when calling startSupportActionMode inside onCreate method?
I'm using appcompat-v7:22.2.0
Toolbar
widget and trying to show the contextual action bar (cab). 我正在使用
appcompat-v7:22.2.0
Toolbar
小部件,并尝试显示上下文操作栏(cab)。
To show this theme problem I wrote 2 methods startSupportActionMode
and startSupportActionModeDelay
. 为了显示此主题问题,我编写了2个方法
startSupportActionMode
和startSupportActionModeDelay
。 The first call AppCompatActivity.startSupportActionMode
immediately from activity onCreate
and the last one wait for a second to call AppCompatActivity.startSupportActionMode
. 第一个从活动
onCreate
立即调用AppCompatActivity.startSupportActionMode
,最后一个等待一秒钟,以调用AppCompatActivity.startSupportActionMode
。
The result is as follows: 结果如下:
Normal Activity 正常活动
Calling startSupportActionModeDelay 调用startSupportActionModeDelay
Calling startSupportActionMode 调用startSupportActionMode
I found many issues related with the CAB background color or suggesting to style it using actionModeStyle
, but this is not what I want. 我发现了许多与CAB背景色有关的问题,或建议使用
actionModeStyle
进行样式actionModeStyle
,但这不是我想要的。
What I wanted is to get always the same result as calling startSupportActionModeDelay
but immediately from onCreate
method (without the need of creating a thread, etc - those are here just to illustrate the problem) because I will have to recreate the context action bar after a orientation change, for example. 我想要的是获得始终与调用
startSupportActionModeDelay
相同的结果,但是立即从onCreate
方法(无需创建线程等,这些只是在说明问题),因为我必须在创建完之后重新创建上下文操作栏例如,方向改变。
Here is the code until now: 这是到目前为止的代码:
RecyclerViewActivity.java RecyclerViewActivity.java
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.ActionMode;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class RecyclerViewActivity extends AppCompatActivity {
private ActionMode.Callback mActionModeCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view);
initToolbar();
initSupportActionMode();
//calling the method below causes the CAB to get a light theme
//startSupportActionMode();
//calling the method below causes the CAB to get a dark theme
//startSupportActionModeDelay();
}
private void initToolbar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
}
}
private void initSupportActionMode() {
mActionModeCallback = new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
}
};
}
private void startSupportActionMode() {
startSupportActionMode(mActionModeCallback);
}
private void startSupportActionModeDelay() {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000l);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
startSupportActionMode(mActionModeCallback);
}
});
}
}).start();
}
}
layout/activity_recycler_view.xml 布局/ activity_recycler_view.xml
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingTop="?attr/actionBarSize"/>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
android:background="?attr/colorPrimary"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:fitsSystemWindows="true">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
values/styles.xml 价值观/ styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="BaseTheme" />
<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorControlHighlight">@color/accent_translucent</item>
<item name="colorAccent">@color/accent</item>
<item name="windowActionModeOverlay">true</item>
</style>
<color name="primary">#00BCD4</color>
<color name="primary_dark">#0097A7</color>
<color name="accent">#FFEB3B</color>
<color name="accent_translucent">#80FFEB3B</color>
<color name="accent_bright">#FFF493</color>
</resources>
In your parent Activity's style add the following items. 在父活动的样式中添加以下项目。
<item name="windowActionModeOverlay">true</item>
<item name="android:actionModeBackground">?attr/colorPrimary</item> //remember this is the colour of your parent activity's toolbar
<item name="android:actionModeStyle">@style/ActionMode</item>
and add the following style. 并添加以下样式。
<style name="ActionMode">
<item name="android:background">?attr/colorPrimary</item>
<item name="android:height">?attr/actionBarSize</item>
</style>
your CAB simply overwrites the actual action bar. 您的CAB只会覆盖实际的操作栏。
Adding the following lines to values/styles.xml
/ BaseTheme
solved the problem: BaseTheme
添加到values/styles.xml
/ BaseTheme
解决了该问题:
<item name="actionBarPopupTheme">@style/ThemeOverlay.AppCompat.Light</item>
<item name="actionBarTheme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
Although I could not figure out what exactly was happening since this themes were already defined on my Toolbar
widget and the setSupportActionBar
was called before startSupportActionMode
. 尽管我无法弄清楚到底发生了什么,因为已经在
Toolbar
小部件上定义了这些主题,并且在setSupportActionBar
之前调用了startSupportActionMode
。
Maybe the context action bar was being created before the styles from Toolbar
widget could be parsed by the setSupportActionBar
. 可能在
setSupportActionBar
可以解析“ Toolbar
小部件中的样式之前创建了上下文操作栏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.