dan*_*sgb 20 android styles android-actionbar searchview
有人知道如何更改appcompat SearchView的文本颜色吗?我花了2个小时从SO那里尝试了几个建议,但都没有.
这是我的代码:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:espacios="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/layer_switcher_button"
android:icon="@drawable/b_categorias"
android:title="@string/menu_layer_switcher_title"
espacios:showAsAction="ifRoom|collapseActionView"/>
<item
android:id="@+id/action_search"
android:icon="@drawable/b_buscar"
android:title="@string/menu_search_title"
espacios:actionViewClass="android.support.v7.widget.SearchView"
espacios:showAsAction="ifRoom|collapseActionView"/>
</menu>
Run Code Online (Sandbox Code Playgroud)
在我的活动中我有这个:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
menuItem layerSwitcher = menu.findItem(R.id.layer_switcher_button);
layerSwitcher.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
onLayerSwitcherButtonClicked();
return false;
}
});
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
LinearLayout ll = (LinearLayout) searchView.getChildAt(0);
TextView textView = (TextView) ll.getChildAt(0);
textView.setTextColor(R.color.white);
}
Run Code Online (Sandbox Code Playgroud)
小智 41
SearchView searchView = new SearchView(getContext());
SearchView.SearchAutoComplete theTextArea = (SearchView.SearchAutoComplete)searchView.findViewById(R.id.search_src_text);
theTextArea.setTextColor(Color.WHITE);//or any color that you want
Run Code Online (Sandbox Code Playgroud)
这些答案都不适合我.最终工作的是在工具栏上设置主题并在那里指定textColorHint.
主题(在styles.xml中):
<style name="ToolbarTheme">
<item name="android:textColorHint">@color/white_opacity_60</item>
</style>
Run Code Online (Sandbox Code Playgroud)
工具栏(任何布局):
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ToolbarTheme" />
Run Code Online (Sandbox Code Playgroud)
这是因为当您将主题设置为ViewGroup时,其属性也会应用于ViewGroup的所有子视图.这是主题和风格之间的差异:)
甲SearchView对象从延伸LinearLayout,所以它保持其它视图.诀窍是找到包含提示文本的视图并以编程方式更改颜色.通过遍历视图层次结构,您可以找到包含提示文本的窗口小部件:
// get your SearchView with its id
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
// traverse the view to the widget containing the hint text
LinearLayout ll = (LinearLayout)searchView.getChildAt(0);
LinearLayout ll2 = (LinearLayout)ll.getChildAt(2);
LinearLayout ll3 = (LinearLayout)ll2.getChildAt(1);
SearchView.SearchAutoComplete autoComplete = (SearchView.SearchAutoComplete)ll3.getChildAt(0);
// set the hint text color
autoComplete.setHintTextColor(getResources().getColor(Color.WHITE));
// set the text color
autoComplete.setTextColor(Color.BLUE);
Run Code Online (Sandbox Code Playgroud)
此方法适用于任何主题.此外,您可以更改SearchView层次结构中其他窗口小部件的外观,例如EditText保留搜索查询.
| 归档时间: |
|
| 查看次数: |
21192 次 |
| 最近记录: |