我已经创建了一个演示,它运行良好。每次显示操作模式时都会调用onPrepareActionMode。在onCreateActionMode之后始终被调用,但如果模式无效,则可能会被多次调用。
[
我请求任何人进行小修改。我需要动态地将状态栏颜色与工具栏颜色相同,您可以看到不必要的Drawyer Layout用于实现此效果,但是如果我删除drawyer layout,则状态栏颜色不会根据工具栏颜色而改变。在实用程序中,您可以看到默认主题颜色为红色,工具栏最初为红色,但状态栏不是,只有当我删除drawyer layout时才是如此。我需要使用style来完成这个任务。
]
创建一个资源布局并将其命名为=> action_mode_activity。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout 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"
android:fitsSystemWindows="true"
android:orientation="vertical"
app:insetForeground="#4000">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<EditText
android:id="@+id/editTextCopy"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_marginTop="19dp"
android:ems="10"
android:inputType="textMultiLine"
android:text="Long click to share!">
<requestFocus />
</EditText>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
创建一个名为ActionModeActivity的活动。
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.example.deepakpawar.demolearning.R;
import com.example.deepakpawar.demolearning.demo.load.recycler.Utils;
public class ActionModeActivity extends AppCompatActivity implements View.OnLongClickListener, ActionMode.Callback {
EditText editTextCopy;
android.view.ActionMode mActionMode;
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Utils.onActivityCreateSetTheme(this);
setContentView(R.layout.action_mode_activity);
editTextCopy = (EditText) findViewById(R.id.editTextCopy);
editTextCopy.setOnLongClickListener(this);
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setTitle("Android Students");
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
}
@Override
public boolean onLongClick(View view) {
if (mActionMode != null) {
return false;
}
mActionMode = this.startActionMode(this);
view.setSelected(true);
return true;
}
@Override
public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.action_menu, menu);
return true;
}
@Override
public boolean onActionItemClicked(android.view.ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.action_share:
Toast.makeText(this, "Shared!", Toast.LENGTH_SHORT).show();
mode.finish();
return true;
default:
return false;
}
}
@Override
public boolean onPrepareActionMode(android.view.ActionMode mode, Menu menu) {
Toast.makeText(ActionModeActivity.this,"onPrepareActionMode Called ",Toast.LENGTH_SHORT).show();
return false;
}
@Override
public void onDestroyActionMode(android.view.ActionMode mode) {
mActionMode = null;
}
}
//Utils类包含用于更改主题的方法
//我创建它是因为需要动态更改应用程序主题
import android.app.Activity;
public class Utils {
private static int sTheme;
public final static int THEME_DEFAULT = 0;
public final static int THEME_WHITE = 1;
public final static int THEME_BLUE = 2;
public static int getsTheme() {
return sTheme;
}
public static void changeToTheme(Activity activity, int theme) {
sTheme = theme;
activity.recreate();
}
public static void onActivityCreateSetTheme(Activity activity) {
switch (sTheme) {
default:
case THEME_DEFAULT:
activity.setTheme(R.style.FirstTheme);
break;
case THEME_WHITE:
activity.setTheme(R.style.SecondTheme);
break;
case THEME_BLUE:
activity.setTheme(R.style.Thirdheme);
break;
}
}
}
v21-themes.xml
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
<item name="android:windowSharedElementExitTransition">@android:transition/move</item>
<item name="android:actionOverflowButtonStyle">@style/Widget.ActionButton.Overflow</item>
<item name="windowActionBar">false</item>
<item name="windowActionModeOverlay">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/PrimaryColor</item>
<item name="colorPrimaryDark">@color/PrimaryDarkColor</item>
<item name="colorAccent">@color/AccentColor</item>
<item name="android:textColorPrimary">@color/TextPrimaryColor</item>
<item name="android:windowBackground">@color/WindowBackground</item>
</style>
<style name="Widget.ActionButton.Overflow" parent="@android:style/Widget.Holo.ActionButton.Overflow">
<item name="android:contentDescription">@string/accessibility_overflow</item>
</style>
<style name="ToolBarStyle" parent="ToolBarStyle.Base" />
<style name="ToolBarStyle.Base" parent="">
<item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
<item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>
<style name="ToolBarStyle.Event" parent="ToolBarStyle">
<item name="titleTextAppearance">@style/TextAppearance.Widget.Event.Toolbar.Title</item>
</style>
<style name="TextAppearance.Widget.Event.Toolbar.Title" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
<item name="android:textStyle">normal</item>
<item name="android:textSize">18sp</item>
<item name="android:textColor">#000000</item>
</style>
<style name="FirstTheme">
<item name="android:textColor">#FF0000</item>
<item name="colorPrimary">#FF0000</item>
<item name="colorPrimaryDark">#ff0000</item>
<item name="colorAccent">#ff0087</item>
<item name="android:shadowColor">#00ccff</item>
<item name="android:shadowRadius">1.5</item>
<item name="android:shadowDy">1</item>
</style>
<style name="SecondTheme">
<item name="android:textColor">#00FF00</item>
<item name="colorPrimary">#00FF00</item>
<item name="colorPrimaryDark">#00FF00</item>
<item name="colorAccent">#00FF90</item>
<item name="android:shadowColor">#00ccff</item>
<item name="android:shadowRadius">1.5</item>
<item name="android:shadowDy">1</item>
</style>
<style name="Thirdheme">
<item name="android:textColor">#0000F0</item>
<item name="colorPrimary">#0000F0</item>
<item name="colorPrimaryDark">#0000F0</item>
<item name="colorAccent">#0090F0</item>
<item name="android:shadowColor">#00ccff</item>
<item name="android:shadowRadius">1.5</item>
<item name="android:shadowDy">1</item>
</style>
<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">#FFCC00</item>
<item name="android:textColorPrimary">#FFFFFF</item>
<item name="android:background">#5fa3d0</item>
</style>
</resources>