问题
I have an Android menu XML that resembles this:-
<item
android:id="@+id/action_share"
android:orderInCategory="100"
android:icon="@drawable/ic_social_share"
app:showAsAction="always"
android:title="" />
<item
android:id="@+id/action_something_else"
android:orderInCategory="200"
android:icon="@drawable/ic_airplanemode_active_black_24dp"
app:showAsAction="always"
android:title="" />
My Android code resembles this:-
private void startActionMode() {
startActionMode(new android.view.ActionMode.Callback2() {
@Override
public boolean onCreateActionMode(final android.view.ActionMode mode, final Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.main, menu);
mode.setTitle("FLOATING!!!!!");
return true;
}
@Override
public boolean onPrepareActionMode(final android.view.ActionMode actionMode, final Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(final android.view.ActionMode actionMode, final MenuItem menuItem) {
return false;
}
@Override
public void onDestroyActionMode(final android.view.ActionMode actionMode) {
}
}, android.view.ActionMode.TYPE_FLOATING);
}
When I deploy my Android application to a Android 8 device the floating action mode shows the icons as required.
However, when I deploy my Android application to a Android 9 device the floating action mode is empty, although there is space for the icons and I can see the background ripple effect where the icons should be.
How do you employ android.view.ActionMode.TYPE_FLOATING
on Android 9 and see the icons for each menu item?
my gradle file resembles this:-
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
buildToolsVersion "28.0.3"
defaultConfig {
applicationId "org.research.development"
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0-alpha4', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'androidx.appcompat:appcompat:1.1.0-alpha02'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
}
heres my applications style
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:windowDisablePreview">true</item>
<item name="windowActionBar">false</item>
<item name="android:textColorPrimary">@android:color/white</item>
<item name="android:textColorSecondary">@android:color/white</item>
<item name="actionMenuTextColor">@android:color/white</item>
<item name="android:windowNoTitle">true</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<!-- It should be true otherwise action mode will not overlay toolbar -->
<item name="windowActionModeOverlay">true</item>
<!-- For Custom Action Mode Background Color/Drawable -->
<item name="actionModeBackground">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
回答1:
class PrimaryActionModeCallback : ActionMode.Callback {
var onActionItemClickListener: OnActionItemClickListener? = null
private var mode: ActionMode? = null
@MenuRes private var menuResId: Int = 0
private var title: String? = null
private var subtitle: String? = null
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
this.mode = mode
mode.menuInflater.inflate(menuResId, menu)
mode.title = title
mode.subtitle = subtitle
return true
}
override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
return false
}
override fun onDestroyActionMode(mode: ActionMode) {
this.mode = null
}
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
onActionItemClickListener?.onActionItemClick(item)
mode.finish()
return true
}
fun startActionMode(view: View,
@MenuRes menuResId: Int,
title: String? = null,
subtitle: String? = null) {
this.menuResId = menuResId
this.title = title
this.subtitle = subtitle
view.startActionMode(this)
}
fun finishActionMode() {
mode?.finish()
}
}
@RequiresApi(Build.VERSION_CODES.M)
class FloatingActionModeCallback : ActionMode.Callback2() {
var onActionItemClickListener: OnActionItemClickListener? = null
private var mode: ActionMode? = null
@MenuRes private var menuResId: Int = 0
private var contentLeft: Int = 0
private var contentTop: Int = 0
private var contentRight: Int = 0
private var contentBottom: Int = 0
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
this.mode = mode
mode.menuInflater.inflate(menuResId, menu)
return true
}
override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
return false
}
override fun onDestroyActionMode(mode: ActionMode) {
this.mode = null
}
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
onActionItemClickListener?.onActionItemClick(item)
mode.finish()
return true
}
override fun onGetContentRect(mode: ActionMode, view: View, outRect: Rect) {
outRect.set(contentLeft, contentTop, contentRight, contentBottom)
}
fun startActionMode(view: View,
@MenuRes menuResId: Int,
contentLeft: Int = 0, contentTop: Int = 0,
contentRight: Int = view.width, contentBottom: Int = view.height) {
this.menuResId = menuResId
this.contentLeft = contentLeft
this.contentTop = contentTop
this.contentRight = contentRight
this.contentBottom = contentBottom
view.startActionMode(this, ActionMode.TYPE_FLOATING)
}
fun finishActionMode() {
mode?.finish()
}
}
interface OnActionItemClickListener {
fun onActionItemClick(item: MenuItem)
}
// Start primary ActionMode
val primaryActionModeCallback = PrimaryActionModeCallback()
primaryActionModeCallback.startActionMode(view, R.menu.menu_actions, "Title", "Subtitle")
// Start floating ActionMode
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val floatingActionModeCallback = FloatingActionModeCallback()
floatingActionModeCallback.startActionMode(view, R.menu.menu_actions, x, y, width, height)
}
You can get description from here,this is working on my Project by some modification.
来源:https://stackoverflow.com/questions/55004998/how-to-display-menu-item-icons-on-android-9-in-floating-action-mode