[英]Removing default search icon from SearchView 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 magImage = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
magImage.setVisibility(View.GONE);
magImage.setImageDrawable(null);
@sector11差不多明白了,但你仍然需要添加magImage.setImageDrawable(null)
因為SearchView.java
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 更改,您的代碼將無法工作。 始終使用標准方法而不是變通方法來解決問題。 有關更多信息,請參閱此鏈接。
對於 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 中搜索視圖的討論很少:
如果要顯示搜索列表,請使用 AutoCompleteTextView 而不是 EditText。
您可以從以下鏈接閱讀教程:
我希望它會起作用。
請在您的代碼中設置此 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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.