其他分享
首页 > 其他分享> > 如何在Floating Action Mode中在Android 9上显示菜单项图标

如何在Floating Action Mode中在Android 9上显示菜单项图标

作者:互联网

我有一个类似于以下的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并查看每个菜单项的图标?

我的gradle文件类似于:

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-9-0-pie,android-actionmode,androidx,android-menu,android
来源: https://codeday.me/bug/20191211/2105915.html