我在我的项目中使用工具栏搜索小部件。一切正常,但期待我完全坚持删除工具栏中搜索字段下方的下划线。我尝试过很多解决方案但没有任何效果。以下是我尝试过的一些解决方案。
要求是删除图像中的白色下划线
是解决方案:
//Removing underline
int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
View searchPlate = searchView.findViewById(searchPlateId);
if (searchPlate!=null) {
searchPlate.setBackgroundColor (Color.TRANSPARENT);
int searchTextId = searchPlate.getContext ().getResources ().getIdentifier ("android:id/search_src_text", null, null);
}
第二解决方案:
EditText searchEdit = ((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text));
searchEdit.setBackgroundColor(Color.TRANSPARENT);
上面的代码用于更改EditText的背景,但仍然下划线显示在SearchBar中的关闭图标下方。
我用于SearchBar小部件的完整代码如下:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater infl) {
super.onCreateOptionsMenu(menu, infl);
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.action_search, menu);
final SearchView searchView = (SearchView) MenuItemCompat
.getActionView(menu.findItem(R.id.search));
SearchManager searchManager = (SearchManager) getActivity ().getSystemService (getActivity ().SEARCH_SERVICE);
searchView.setSearchableInfo (searchManager.getSearchableInfo (getActivity ().getComponentName ()));
//changing edittext color
EditText searchEdit = ((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text));
searchEdit.setTextColor(Color.WHITE);
}
action_search.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:compat="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/search"
android:title="Search"
android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
compat:showAsAction="ifRoom|collapseActionView"
compat:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
可能与this重复
在此先感谢,任何类型的解决方案和建议对我来说都非常有用。
一旦尝试如下
View v = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
v.setBackgroundColor(Color.parseColor("here give actionbar color code"));
希望这会对你有所帮助。
如果您使用的是SearchView,请使用21以下的API
android:queryBackground="@android:color/transparent"
如果您使用的是API 21或更高版本,则可以使用以下代码
app:queryBackground="@android:color/transparent"
或者您可以通过样式执行此操作:
<style name="SearchViewMy" parent="Widget.AppCompat.Light.SearchView">
<item name="submitBackground">@color/primaryColor</item>
<item name="queryBackground">@color/primaryColor</item>
</style>
queryBackground是查询背景的属性。如果您支持语音搜索,您可能还想删除该麦克风按钮下的线路。这是submitBackground属性的用途。
然后当然将样式应用于您的主题。
<style name="Theme.App" parent="Theme.AppCompat.Light.NoActionBar.FullScreen">
<item name="searchViewStyle">@style/SearchViewMy</item>
</style>
如果您使用的是v7小部件,只需添加此行即可
应用程式:queryBackground = “@机器人:彩色/透明”
您可以尝试使用以下代码,对我来说就像一个魅力。
View v = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
v.setBackgroundColor(ContextCompat.getColor(context,android.R.color.transparent));
对于AndroidX,我在此基于其他答案使用了这个,
mSearchView.findViewById(androidx.appcompat.R.id.search_plate)
.setBackgroundColor(Color.TRANSPARENT);
用于更改SearchView编辑文本背景颜色的代码如下所示
public static void setSearchViewEditTextBackgroundColor(Context context, SearchView searchView, int backgroundColor){
int searchPlateId = context.getResources().getIdentifier("android:id/search_plate", null, null);
ViewGroup viewGroup = (ViewGroup) searchView.findViewById(searchPlateId);
viewGroup.setBackgroundColor(ComponentUtils.getColor(context, backgroundColor));
}
如果操作栏背景颜色为白色,请按以下方式调用上述方法。
setSearchViewEditTextBackgroundColor(this, searchView, R.color.white);
现在,SearchView编辑文本的下划线将消失。
以下为我完美地解决了这个问题:
android:background="@android:color/transparent"