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

自定义SearchView的搜索图标和字体属性相对复杂一些,记下来。

一、自定义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));
}
三、最终效果图:

				
时间: 2024-08-25 10:58:32

Android SearchView 自定义SearchIcon和字体颜色大小的相关文章

Android NumberPicker控件修改分割线颜色及字体颜色大小

(1)重写NumberPicker已达到修改显示字体颜色大小 public class TextColorNumberPicker extends NumberPicker { public TextColorNumberPicker(Context context) { super(context); } public TextColorNumberPicker(Context context, AttributeSet attrs) { super(context, attrs); } pu

用UISlider控制自定义tableViewCell中字体的大小

字体随滑杆的滑动而变大或变小.用滑杆去控制字体的大小很简单,但用字体去控制自定义tableViewcell内字体的大小也很简单,只不过第一次做的时候可能要摸索一下,这里我就简单的给大家演示一下: 这是一个按钮,点击按钮弹出一个带有slider的view - (IBAction)btnClick:(id)sender { UIView *sliderView = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width-1

自定义Label的字体颜色和大小

废话不多说,直接上代码,很简单,都是原生的,注意一点就是label不能用.text,要用.attributedText!! #import "ViewController.h" @interface ViewController () <UITableViewDataSource,UITableViewDelegate>{    NSMutableArray *_fontArray;} @property (weak, nonatomic) IBOutlet UITable

Android开发在string.xml文件中设置部分字体颜色大小

1.在string.xml文件中: <string name="exchange_txt_hint"><Data><![CDATA[请使用<font color="#ff0000"><b>%1$s</b></font>牛兑换<font color="#00ff00"><b>%2$s</b></font>%3$s 上网时长]

Android中string.xml文件中设置部分字体颜色大小

1.在string.xml文件中: <string name="tips_all"><Data><![CDATA[清理进程:<font color="#7700ff00"><b>%1$d<br/></b></font>清理内存:<font color="#7700ff00"><b>%2$sMB</b></font&g

iOS 界面上绘制不同字体 颜色 大小的字符串

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAttributed String"]; //颜色-范围 [str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,5)]; [str addAttribute:NSFor

改变placeholder的字体颜色大小

input::-webkit-input-placeholder { /* WebKit browsers */ font-size:14px; color: #333; } input::-moz-placeholder { /* Mozilla Firefox 19+ */ font-size:14px; color: #333; } input:-ms-input-placeholder { /* Internet Explorer 10+ */ font-size:14px; color

Android Them+SharedPreferences 修改程序所有view字体颜色、大小和页面背景

有这么一个需求,可以对页面的样式进行选择,然后根据选择改变程序所有字体颜色和页面背景.同时下一次启动程序,当前设置依然有效. 根据需求,我们需要一种快速,方便,有效的方式来实现需求,然后可以通过Android Them + SharedPreferences 来实现需求.Them用于存放设置的每一种样式,并应用于程序中,SharedPreferences用于记住程序当前的样式,根据SharedPreferences的内容来设置程序的样式,实现下次启动能够oncreat当前的样式设置. 这里的Th

(转)Android 自定义 spinner (背景、字体颜色)

Android 自定义 spinner (背景.字体颜色) (2012-07-04 17:04:44)   1.准备两张图片,并做好9.png 2.在drawable中定义spinner_selector.xml <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" &