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

时间:2019-03-05 14:23:37

标签: android android-menu androidx android-9.0-pie android-actionmode

我有一个类似于以下的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" />

0 个答案:

没有答案