【问题标题】:Long press Toolbar menu icon show gray text background长按工具栏菜单图标显示灰色文本背景
【发布时间】:2020-02-03 10:19:50
【问题描述】:

我有工具栏和带有图标和说明的菜单项。

当我长按该图标时,会出现一个带有该菜单项描述的弹出窗口。

问题在于该弹出窗口中的文本颜色与背景颜色不同。

这里是 toolbar.xml 文件代码。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    style="@style/ToolbarThemeMain"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:minHeight="?attr/actionBarSize"
    android:theme="?attr/toolbarStyle"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

这是 styles.xml

toolbarStyle 的代码
  <style name="ToolbarThemeMain" parent="Theme.AppCompat.Light">
    <item name="android:textColorPrimary">@color/textLight</item>
    <item name="android:textColorSecondary">@color/textSecondary</item>
    <item name="actionMenuTextColor">@color/white</item>
    <item name="colorControlNormal">@color/white</item>
    <item name="android:background">@color/colorPrimary</item>
    <item name="android:actionMenuTextColor">@color/white</item>
    <item name="colorControlHighlight">@color/colorPrimaryDark</item>
    <item name="colorControlActivated">@color/colorPrimaryDark</item>
    <item name="android:drawSelectorOnTop">true</item>
  </style>

colorPrimary 是上图中的绿色。

如何解决这个问题,去掉灰色的文字背景,变成和colorPrimary一样的绿色?

【问题讨论】:

    标签: android android-toolbar android-menu android-touch-event


    【解决方案1】:

    这根本与工具栏无关,你必须改变你的弹出窗口,据我所知,制作你自己的弹出窗口布局,我不知道你有没有,但我还是把代码放在这里:

    长按图标时首先调用此弹出窗口:

    private void setPopUpWindow() {
        LayoutInflater inflater = (LayoutInflater)
                getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.pop_up_menu, null);
        mypopupWindow = new PopupWindow(view, 50, 20, true);//set the size of your popup here (remember you can suite the sizes based on the phones's width and height)
        mypopupWindow.setOutsideTouchable(true);
        mypopupWindow.setFocusable(true);
        mypopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//this is for disappearing the popup after somewhere else has touched
    }
    

    现在您可以在布局文件夹中的 pop_up_menu.xml 中按照您想要的方式更改弹出窗口:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@color/white"
        android:gravity="center">
            <TextView
                android:padding="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="text here"
                android:fontFamily="@font/font"
                android:background="@color/green"
                android:textColor="@color/black"></TextView>
    </LinearLayout>
    

    如有任何问题,请随时提出... :)

    【讨论】:

    • 这可行,但我认为应该有一个更简单、更优雅的解决方案来覆盖菜单项的文本背景颜色。
    猜你喜欢
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 2015-11-01
    • 2016-12-11
    • 1970-01-01
    相关资源
    最近更新 更多