Changing action bar searchview hint text color

后端 未结 18 1372
时光说笑
时光说笑 2020-12-12 21:47

How can I change the action bar search view hint text colour?

This question explains how to get the EditText when using ABS: Android ActionBar Customize Search View

18条回答
  •  伪装坚强ぢ
    2020-12-12 22:46

    Look to this class, it can help you to solve your problem

    import android.R.color;
    import android.content.Context;
    import android.graphics.Typeface;
    import android.widget.ImageView;
    
    import com.actionbarsherlock.widget.SearchView;
    import com.actionbarsherlock.widget.SearchView.SearchAutoComplete;
    
    public class MySearchView {
    
        public static SearchView getSearchView(Context context, String strHint) {
            SearchView searchView = new SearchView(context);
            searchView.setQueryHint(strHint);
            searchView.setFocusable(true);
            searchView.setFocusableInTouchMode(true);
            searchView.requestFocus();
            searchView.requestFocusFromTouch();
    
            ImageView searchBtn = (ImageView) searchView.findViewById(R.id.abs__search_button);
            searchBtn.setImageResource(R.drawable.ic_menu_search);
    
            // ImageView searchBtnClose = (ImageView) searchView.findViewById(R.id.abs__search_close_btn);
            // searchBtnClose.setImageResource(R.drawable.ic_cancel_search_bar);
    
    
            SearchAutoComplete searchText = (SearchAutoComplete) searchView.findViewById(R.id.abs__search_src_text);
            setFont(context, searchText);
            searchText.setTextColor(context.getResources().getColor(color.white));
            searchText.setHintTextColor(context.getResources().getColor(color.white));
    
            return searchView;
        }
    
        private static void setFont(Context _context, SearchAutoComplete searchText) {
            Typeface tf = null;
            Typeface currentType = searchText.getTypeface();
            if (currentType == null) {
                tf = MyApplication.fontNormal;
            }
            else {
                int currentStyle = currentType.getStyle();
                if (currentStyle == Typeface.BOLD) {
                    tf = MyApplication.fontBold;
                }
                else if (currentStyle == Typeface.BOLD_ITALIC) {
                    tf = MyApplication.fontBoldItalic;
                }
                else if (currentStyle == Typeface.ITALIC) {
                    tf = MyApplication.fontItalic;
                }
                else {
                    tf = MyApplication.fontNormal;
                }
            }
            searchText.setTypeface(tf);
        }
    
        public static SearchView getSearchView(Context context, int strHintRes) {
            return getSearchView(context, context.getString(strHintRes));
        }
    }
    

提交回复
热议问题