Android SearchView 自定义SearchIcon和字体颜色大小

一、自定义SearchIcon

1、API版本低于21:版本小于21时,要修改SearchIcon比较复杂,需要先获取到SearchView的ImageView,然后为ImageView设置图片,具体代码如下:

(1)初始化SearchView控件

mSearch = (SearchView) view.findViewById(R.id.search);

(2)设置自定义的搜索图标

复制代码
if(mSearch==null){
    return;
}else{
//获取ImageView的id
int imgId = mSearch.getContext().getResources().getIdentifier("android:id/search_mag_icon",null,null); 
//获取ImageView
ImageView searchButton = (ImageView)mSearch.findViewById(imgId);
//设置图片
searchButton.setImageResource(R.drawable.search);
//不使用默认
mSearch.setIconifiedByDefault(false);
}
复制代码
2、API版本大于21时,就很方便了,直接在layout文件中为SearchView设置属性searchIcon即可啦。
android:searchIcon="@drawable/search"
二、自定义字体颜色和大小,也可以修改SearchView中的提示文字的颜色

1、初始化SearchView控件,同上。

2、获取到SearchView的TextView,然后就可以修改其属性了,代码如下。

复制代码
if(mSearch==null){
    return;
}
else{
//获取到TextView的ID
int id = mSearch.getContext().getResources().getIdentifier("android:id/search_src_text",null,null);
//获取到TextView的控件
TextView textView = (TextView) mSearch.findViewById(id);
//设置字体大小为14sp
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);//14sp
//设置字体颜色
textView.setTextColor(getActivity().getResources().getColor(R.color.search_txt_color)); 
//设置提示文字颜色
textView.setHintTextColor(getActivity().getResources().getColor(R.color.search_hint_color));
}
复制代码
三、最终效果图:



转自:http://www.cnblogs.com/LT5505/p/5534357.html

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
To move the search icon to the right in an Android SearchView, you can use the following steps: 1. Create a new XML file named `searchview_layout.xml` in your `res/layout` folder. 2. Add the following code to the XML file: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <SearchView android:id="@+id/search_view" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:iconifiedByDefault="false" android:queryHint="Search" android:layout_gravity="center_vertical" /> <ImageView android:id="@+id/search_close_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_close" android:layout_gravity="center_vertical" /> </LinearLayout> ``` 3. In your Java code, you can inflate this layout and use the `SearchView` and `ImageView` as you would normally. For example: ``` SearchView searchView = findViewById(R.id.search_view); ImageView closeButton = findViewById(R.id.search_close_btn); // Move the search icon to the right int searchIconId = searchView.getContext().getResources() .getIdentifier("android:id/search_mag_icon", null, null); ImageView searchIcon = searchView.findViewById(searchIconId); searchIcon.setLayoutParams(new LinearLayout.LayoutParams(0, 0)); // Set the close button to close the search view closeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { searchView.setQuery("", false); searchView.clearFocus(); } }); ``` This code will move the search icon to the right and add a close button to the left of the search view. When the close button is clicked, the search view will be cleared and the focus will be removed.
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值