我该如何更改android.support.v7.widget.SearchView的提示文本颜色

时间:2018-02-01 11:12:47

标签: android material-design searchview

如何更改android.support.v7.widget.SearchView的提示文字颜色?

enter image description here

我看到了很多答案,但没有人给出如何更改搜索视图的提示文字颜色

<android.support.v7.widget.SearchView
        android:id="@+id/search"
        style="@style/SearchViewStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColorHint="@android:color/black"
        android:clickable="true"
        android:focusable="true"/>

活动代码:

rootView = inflater.inflate(R.layout.list_of_complaints, container, false);

//calling in init view method
initView();

//getting complaints from server
myHelpRequest = new MyHelpRequest(listView, search, getActivity());

//setting progress bar visible to know user something is loading
progressbar.setVisibility(View.VISIBLE);

//set adapter for list items
myHelpRequest.getAllComplaintsFromServer(progressbar);
//search view listener logic
search.setActivated(true);
search.setQueryHint("search");
search.setQueryHint(Html.fromHtml("<font color = #ffffff>" + "search for quick access" + "</font>"));
search.onActionViewExpanded();
search.setIconified(false);
search.clearFocus();

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Toast.makeText(getContext(),i+" item clicked",Toast.LENGTH_LONG).show();
        TextView text = (TextView)view.findViewById(R.id.help_id);
        String message= text.getText().toString();
        Toast.makeText(getActivity(),"helpID"+ message,  Toast.LENGTH_LONG).show();
        Intent intent=new Intent(getActivity(),MyHelpReqDetailsActivity.class);
        intent.putExtra("helpID",message);
        startActivity(intent);
    }
});

return rootView;
}

3 个答案:

答案 0 :(得分:2)

初始化

SearchView searchView = findViewById(R.id.viewSearch);
    searchView.setQueryHint("Search For Anything"); ---hint that is to be displayed
    LinearLayout first = (LinearLayout)searchView.getChildAt(0);
    LinearLayout second = (LinearLayout)first.getChildAt(2);
    LinearLayout third = (LinearLayout)second.getChildAt(1);
**strong text**    
SearchView.SearchAutoComplete autoComplete = (SearchView.SearchAutoComplete)third.getChildAt(0);

-------这将更改查询提示颜色

autoComplete.setHintTextColor(getResources().getColor(R.color.gray2));

-----这将更改查询文本的颜色

autoComplete.setTextColor(getResources().getColor(R.color.black));

答案 1 :(得分:0)

尝试此操作以更改searchView提示颜色

<强> Style.xml

<style name="SearchViewTheme" parent="Widget.AppCompat.Light.SearchView">
<item name="android:maxWidth">10000dp</item>
<item name="android:textColorHint">@android:color/white</item>

searchView

<android.support.v7.widget.SearchView
    android:id="@+id/search"
    style="@style/SearchViewStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
   android:theme="@style/SearchViewTheme"
    android:clickable="true"
    android:focusable="true"/>

或者,尝试通过java设置HintColor:

search = (SearchView) rootView.findViewById(R.id.search);
    searchEditText = (EditText)search.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    searchEditText.setTextColor(getResources().getColor(R.color.colorPrimary));
    searchEditText.setHintTextColor(getResources().getColor(R.color.colorPrimary));

答案 2 :(得分:0)

是的,我通过使用代码中的更改来解决我的问题

 search = (SearchView) rootView.findViewById(R.id.search);
    // search.setQueryHint("search");
    searchEditText = (EditText)search.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    searchEditText.setTextColor(getResources().getColor(R.color.colorPrimary));
    searchEditText.setHintTextColor(getResources().getColor(R.color.colorPrimary));
    searchEditText.setHint("search here for quick access");

我评论search.setQueryHint(&#34; search&#34;);方法。然后我访问searchview的EditText,并调用searchEditText.setHint(&#34;在这里搜索以便快速访问&#34;);