Android 如何将工具栏SearchView下划线颜色更改为灰色
当我们展开搜索栏时,我试图更改搜索视图的下划线颜色`Android 如何将工具栏SearchView下划线颜色更改为灰色,android,xml,toolbar,android-toolbar,searchview,Android,Xml,Toolbar,Android Toolbar,Searchview,当我们展开搜索栏时,我试图更改搜索视图的下划线颜色` SearchView.SearchAutoComplete autoCompleteTextView = (SearchView.SearchAutoComplete) searchView.findViewById(R.id.search_src_text); autoCompleteTextView.setCursorVisible(true); autoCompleteTextView.setTextColor(
SearchView.SearchAutoComplete autoCompleteTextView = (SearchView.SearchAutoComplete) searchView.findViewById(R.id.search_src_text);
autoCompleteTextView.setCursorVisible(true);
autoCompleteTextView.setTextColor(ContextCompat.getColor(this, R.color.textcolor_light_gray));
autoCompleteTextView.setHintTextColor(autoCompleteTextView.getTextColors().withAlpha(255));
autoCompleteTextView.setTextSize(16);
View searchplate = (View)searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
searchplate.setBackgroundResource(R.drawable.autocomplete_bg);
在这里,我使用的主题,以改变颜色也
<style name="MySearchViewStyle" parent="Widget.AppCompat.Light.SearchView">
<!-- Background for the search query section (e.g. EditText) -->
<item name="queryBackground">@color/darkgray</item>
</style>
@彩色/暗光
但对我来说没什么用。有人帮我吗…我终于找到了解决办法
View searchplate = (View)searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
searchplate.getBackground().setColorFilter(Color.DKGRAY, PorterDuff.Mode.MULTIPLY);
您也可以通过xml对其进行更改:
<item name="android:queryBackground">...</item>
。。。
我猜你忘记了android:前缀下面的示例对我有用,我希望它也对你有用
<SearchView
android:id="@+id/search_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:iconifiedByDefault="false"
android:gravity="center"
android:background="@drawable/bg_searchview"
android:queryBackground="@drawable/bg_searchview_query"/>
android:queryBackground=“@drawable/bg\u searchview\u query”
在drawable中,为背景创建.xml文件bg_searchview
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<padding
android:top="10dp"
android:bottom="10dp" />
<solid android:color="#FFFFFF" />
<corners android:radius="20dp"/>
<stroke
android:width="0.3dp"
android:color="#797979" />
</shape>
在drawable中,为queryBackground创建.xml文件bg\u searchview\u query
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<corners android:radius="20dp"/>
</shape>
我们可以通过xml做同样的事情吗?主题还是样式?不。我只尝试了主题或样式。但没有效果。所以只有我添加了颜色filter@sreegetBackground()返回null@ArturA,我们需要在使用getBackground()之前设置背景。这里我使用searchplate.setBackgroundResource(R.drawable.autocomplete\u bg);必须在顶部添加PorterDuff.Mode.SRC_以丢弃下划线的原始基色并使用新的基色。