android - 在 MaterialToolbar 中更改 Searchview 的文本颜色不起作用

标签 android android-layout android-toolbar searchview android-theme

我有一个 material.appbar 布局,我在菜单中有一个搜索 View 。我正在尝试更改搜索 View 中文本的颜色,但它对我不起作用。我已经尝试过这些 posts 中提到的现有解决方案他们没有为我工作。有帮助吗?

我的代码:

<com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppBarOverlay">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/topAppBar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:title="@string/home_page_title"
            app:subtitle="@string/home_page_subtitle"
            app:menu="@menu/top_app_bar"
            app:navigationIcon="@drawable/ic_baseline_menu"
            style="@style/searchview"
            />

    </com.google.android.material.appbar.AppBarLayout>


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/search"
        android:icon="@drawable/search"
        android:title="@string/search"
        android:contentDescription="@string/content_description_search"
        app:actionViewClass="androidx.appcompat.widget.SearchView"
        app:showAsAction="ifRoom" />

</menu>

<style name="searchview" parent="Widget.MaterialComponents.Toolbar.Primary">
        <item name="android:textColorPrimary">@android:color/white</item>
        <item name="android:textColorSecondary">@android:color/white</item>
        <item name="android:editTextColor">@android:color/white</item>
        <item name="android:textSize">28sp</item>
    </style>

最佳答案

您可以使用:

    <com.google.android.material.appbar.MaterialToolbar
        android:theme="@style/ThemeOverlayToobar"
        ...>

与:

<style name="ThemeOverlayToobar" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
    <item name="android:editTextColor">@color/....</item>
</style>

enter image description here

如果您还想更改提示文本颜色:

<style name="ThemeOverlayToobar" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
    <item name="android:editTextColor">@color/...</item>
    <item name="android:textColorHint">@color/...</item>
</style>

enter image description here

关于android - 在 MaterialToolbar 中更改 Searchview 的文本颜色不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62947135/

相关文章:

java - Android VideoView 在 onPrepared 中崩溃

android - 将 Android 移植到新的 LWUIT 1.5

android - CardView中不必要的填充?

android - 使用 OnGestureListener 或 OnGesturePerformedListener 在 XML 布局之间滑动

android - 为什么 CollapsingToolbarLayout 在扩展内容下放置一个空白空间

Android - 将 ActionBar 后退按钮切换为导航按钮

android - 升级到 Support Library v21 后,ActionBar aka Toolbar 为黑色

java - 如果函数多次附加加载消息

android - 是否可以将 cameraX 或 Camera2 缩放到 Api 最大缩放限制之外

android - android中的RelativeLayout layout_below问题