gpt4 book ai didi

android - 如何在 float 操作模式下在Android 9上显示菜单项图标

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:10:26 26 4
gpt4 key购买 nike

我有一个android菜单xml类似于这样:

<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="" />

我的android代码类似于这样:
   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);
}

当我将我的android应用程序部署到android 8设备时,浮动操作模式会根据需要显示图标。
然而,当我将我的android应用程序部署到android 9设备上时,浮动操作模式是空的,尽管图标有空间,而且我可以看到图标应该在的地方的背景涟漪效果。
如何在android 9上使用 android.view.ActionMode.TYPE_FLOATING并查看每个菜单项的图标?
我的毕业档案像这样:
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'
}

这是我的申请风格
<!-- 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" />

最佳答案

    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,这是通过一些修改来处理我的项目。

关于android - 如何在 float 操作模式下在Android 9上显示菜单项图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55004998/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com