【发布时间】:2015-04-09 05:50:48
【问题描述】:
如何删除在 SearchView 小部件中显示为提示的默认搜索图标(不在操作栏中)?通过样式我可以更改图标但我找不到删除它的方法?
【问题讨论】:
标签: android android-widget searchview
如何删除在 SearchView 小部件中显示为提示的默认搜索图标(不在操作栏中)?通过样式我可以更改图标但我找不到删除它的方法?
【问题讨论】:
标签: android android-widget searchview
成功了……
ImageView searchViewIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
ViewGroup linearLayoutSearchView =(ViewGroup) searchViewIcon.getParent();
linearLayoutSearchView.removeView(searchViewIcon);
【讨论】:
ImageView searchViewIcon = (ImageView)searchView.findViewById(androidx.appcompat.R.id.search_mag_icon); searchViewIcon.setVisibility(View.GONE);
对我来说,它只起作用:
ImageView magImage = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
magImage.setVisibility(View.GONE);
magImage.setImageDrawable(null);
@sector11's 几乎搞定了,但是你仍然需要添加行 magImage.setImageDrawable(null) 因为在 Github 中查看 SearchView.java,特别是方法 updateViewsVisibility,你可以看到它不断更新其可见性
从 GitHub 提取
if (mCollapsedIcon.getDrawable() == null || mIconifiedByDefault) {
iconVisibility = GONE;
} else {
iconVisibility = VISIBLE;
}
mCollapsedIcon.setVisibility(iconVisibility);
【讨论】:
现在你应该使用
ImageView magImage = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
magImage.setVisibility(View.GONE);
【讨论】:
试试这个:
<style name="SearchViewStyle" parent="Widget.AppCompat.SearchView" >
<item name="searchHintIcon">@null</item>
<!-- Background for the search query section (e.g. EditText) -->
<item name="queryBackground">@color/transparent</item>
</style>
永远不要使用直接的 android id 来查找视图,因为将来如果 id 发生变化,您的代码将无法工作。始终使用标准方法而不是变通方法来解决问题。 更多信息请参考link。
【讨论】:
对于 androidx SearchView 只需使用 app:searchIcon="@null"
【讨论】:
你可以覆盖它:
int searchImgId = context.getResources().getIdentifier("android:id/search_mag_icon", null, null);
ImageView searchImage = (ImageView) searchView.findViewById(searchImgId);
searchImage.setVisibility(View.GONE);
如果您的搜索视图在布局中,请使用带有关闭按钮的 EditText:
如果您想创建自己的,请使用框架布局并添加文本更改侦听器以管理关闭或取消按钮的可见性。
关于 UI 中的搜索视图的讨论很少:
How to create EditText with cross(x) button at end of it?
How to place button inside of edit text
如果要显示搜索列表,请使用 AutoCompleteTextView 而不是 EditText。
您可以从以下链接阅读教程:
我希望它会起作用。
【讨论】:
searchImage.setLayoutParams.. 上给出 NullPointerException,我把它放在活动的 onCreate() 中
searchImage.setVisibility(View.GONE); 上获得空指针异常
android:id/search_mag_icon 是正确的吗?
setIconifiedByDefault(false)
请在您的代码中设置此 SearchView
android:paddingLeft="-16dp" // this line is remove the space of icon
android:paddingStart="-16dp" // // this line is remove the space of icon
android:searchIcon="@null" // this line to remove the search icon
android:closeIcon="@null" // this line to remove the close icon
<SearchView
android:queryBackground="@android:color/transparent"
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="-16dp"
android:paddingStart="-16dp"
android:layout_gravity="center"
android:gravity="center"
android:background="@null"
android:searchIcon="@null"
android:closeIcon="@null"
android:theme="@style/BaseTheme"
android:iconifiedByDefault="false"
android:queryHint="@string/hint">
<requestFocus />
</SearchView>
【讨论】: