Java 为什么在onCreate方法中调用startSupportActionMode时上下文操作栏的样式会不同?
我正在使用appcompat-v7:22.2.0Java 为什么在onCreate方法中调用startSupportActionMode时上下文操作栏的样式会不同?,java,android,toolbar,contextual-action-bar,Java,Android,Toolbar,Contextual Action Bar,我正在使用appcompat-v7:22.2.0工具栏小部件并尝试显示上下文操作栏(cab) 为了显示这个主题问题,我编写了两个方法startSupportActionMode和startSupportActionModeDelay。第一次调用AppCompatActivity.startSupportActionMode时,立即从activityonCreate调用,最后一次等待一秒钟调用AppCompatActivity.startSupportActionMode 结果如下: 正常活动
工具栏
小部件并尝试显示上下文操作栏(cab)
为了显示这个主题问题,我编写了两个方法startSupportActionMode
和startSupportActionModeDelay
。第一次调用AppCompatActivity.startSupportActionMode
时,立即从activityonCreate
调用,最后一次等待一秒钟调用AppCompatActivity.startSupportActionMode
结果如下:
正常活动
呼叫startSupportActionModeDelay
调用startSupportActionMode
我发现许多与CAB背景颜色相关的问题,或者建议使用actionModeStyle
,但这不是我想要的
我想要得到的结果总是与调用startSupportActionModeDelay
相同,但立即从onCreate
方法(无需创建线程等-这些只是为了说明问题),因为例如,在方向更改后,我必须重新创建上下文操作栏
以下是到目前为止的代码:
RecycleServiceActivity.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\u recycler\u 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>
<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>
values/styles.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>
<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>
@颜色/原色
@颜色/原色/深色
@颜色/重音为半透明
@颜色/口音
真的
#00BCD4
#0097A7
#FFEB3B
#80FFEB3B
#FFF493
将以下行添加到values/styles.xml
/BaseTheme
解决了问题:
<item name="actionBarPopupTheme">@style/ThemeOverlay.AppCompat.Light</item>
<item name="actionBarTheme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
@style/ThemeOverlay.AppCompat.Light
@style/ThemeOverlay.AppCompat.Dark.ActionBar
尽管我无法弄清楚到底发生了什么,因为这个主题已经在我的工具栏
小部件上定义,并且设置支持操作栏
在开始支持操作模式
之前被调用
在父活动样式中的
setSupportActionBar
可以解析来自工具栏的样式之前,可能创建了上下文操作栏。请添加以下项目
<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>
true
?attr/colorPrimary//记住这是父活动工具栏的颜色
@风格/动作模式
并添加以下样式
<style name="ActionMode">
<item name="android:background">?attr/colorPrimary</item>
<item name="android:height">?attr/actionBarSize</item>
</style>
?属性/颜色初级
?属性/操作栏大小
您的CAB只会覆盖实际操作栏