黑色背景下Android SearchView图标与查询文本不可见的解决方法
问题描述
当布局背景设置为黑色时,Android的SearchView组件的图标、文本以及查询文本均无法显示。尝试应用自定义样式但未生效,推测该样式是针对v7版本组件设计的。
布局代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activityGiphy" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/black" android:orientation="vertical" tools:context=".presentation.ui.view.GiphyActivity"> <SearchView android:id="@+id/svGiphy" style="@style/AppSearchView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginHorizontal="15dp" android:layout_marginTop="15dp" android:layout_marginBottom="15dp" android:background="@drawable/search_shape" android:iconifiedByDefault="false" android:queryHint="input text" /> </LinearLayout>
样式代码
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="AppSearchView" parent="Widget.AppCompat.Light.SearchView" > <item name="android:textColorPrimary">@color/white</item> <item name="android:textColorHint">@color/white</item> <item name="android:tint">@color/colorPrimary</item> <item name="android:panelColorForeground">@color/white</item> </style> </resources>
Shape代码
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="16dp"/> <stroke android:color="@color/white" android:width="2dp"/> </shape>
效果对比
- 黑色背景效果:

- 白色背景效果:

解决方案
1. 替换为AppCompat版本SearchView
布局中使用androidx.appcompat.widget.SearchView替代原生SearchView,确保和你的AppCompat样式匹配:
<androidx.appcompat.widget.SearchView android:id="@+id/svGiphy" style="@style/AppSearchView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginHorizontal="15dp" android:layout_marginTop="15dp" android:layout_marginBottom="15dp" android:background="@drawable/search_shape" android:iconifiedByDefault="false" android:queryHint="input text" />
2. 修正样式属性(移除android:前缀)
AppCompat组件的样式属性无需android:前缀,调整后样式如下:
<style name="AppSearchView" parent="Widget.AppCompat.Light.SearchView" > <item name="textColorPrimary">@color/white</item> <item name="textColorHint">@color/white</item> <item name="tint">@color/white</item> <!-- 将图标色调设为白色 --> <item name="panelColorForeground">@color/white</item> </style>
3. 代码兜底设置(样式不生效时用)
如果上述方法无效,可在代码中直接修改控件颜色:
val searchView = findViewById<androidx.appcompat.widget.SearchView>(R.id.svGiphy) // 设置文本和提示文字颜色 val searchEditText = searchView.findViewById<EditText>(androidx.appcompat.R.id.search_src_text) searchEditText.setTextColor(Color.WHITE) searchEditText.setHintTextColor(Color.WHITE) // 设置图标颜色 val searchIcon = searchView.findViewById<ImageView>(androidx.appcompat.R.id.search_mag_icon) searchIcon.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN) val clearIcon = searchView.findViewById<ImageView>(androidx.appcompat.R.id.search_close_btn) clearIcon.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN)
4. 确认Activity主题
确保Activity主题继承自AppCompat系列主题,比如Theme.AppCompat.Light.NoActionBar或Theme.MaterialComponents.DayNight.NoActionBar,保证AppCompat组件样式正常生效。
内容的提问来源于stack exchange,提问作者Talkative Breakdown
相关产品推荐
相关产品推荐

