修改searchView属性

@SuppressLint("NewApi")
private void initSearchView(SearchView mSearchView){
 Class<?> argClass = mSearchView.getClass(); 
      
try {

             
           //注意mSearchPlate的背景是stateListDrawable(不同状态不同的图片)  所以不能用BitmapDrawable  
           Field ownField = argClass.getDeclaredField("mSearchPlate");  
           //setAccessible 它是用来设置是否有权限访问反射类中的私有属性的,只有设置为true时才可以访问,默认为false  
           ownField.setAccessible(true);  
           View mView;

mView = (View) ownField.get(mSearchView);

//            mView.setBackground(getResources().getDrawable(R.drawable.person_edittext_selector));
           mView.setBackgroundColor(Color.TRANSPARENT);  
           
           //修改为展开时的搜索图标  
           Field mSearchButton = argClass.getDeclaredField("mSearchButton");  
           mSearchButton.setAccessible(true);  
           ImageView search = (ImageView) mSearchButton.get(mSearchView);  
           search.setImageResource(R.drawable.hy_ser); 
           
           //指定某个私有属性  
           Field mSearchHintIconField = argClass.getDeclaredField("mSearchHintIcon");  
           mSearchHintIconField.setAccessible(true);  
           ImageView mSearchHintIcon = (ImageView)mSearchHintIconField.get(mSearchView);  
//            mSearchHintIcon.setVisibility(View.GONE);  
           mSearchHintIcon.setImageResource(R.drawable.hy_ser);  
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}  catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}   
          
}

你可能感兴趣的:(修改searchView属性)