改变光标颜色搜索查看无ActionBarSherlock [英] Changing the cursor color in SearchView without ActionBarSherlock
问题描述
我试图改变闪烁光标的颜色对搜索查看小部件ICS +。我已经试过如下:
I am attempting to change the color of the blinking cursor on the SearchView widget in ICS+. I have tried the following:
- 添加
<项目名称=机器人:textCursorDrawable> @空< /项目>
来我 主题 - 添加为AutoCompleteTextViews一个风格,我的主题,并设置该样式的textCursorAttribute到@null
- 设置
安卓textCursorDrawable =@空
直接在搜索查看
- Adding
<item name="android:textCursorDrawable">@null</item>
to my theme - Adding a style for AutoCompleteTextViews to my theme and setting the textCursorAttribute of that style to @null
- Setting
android:textCursorDrawable="@null"
directly on the SearchView
我在这里找到了答案(在搜索查看 自定义光标颜色),但有不是searchAutoCompleteTextView非ABS风格,所以无法尝试。我也找了一个Java方法来设置文本光标绘制,但无法找到一个 - 我修改Java中的搜索查看其它方面将能够与光标这样做,如果有可用的方法。
I read the answer here (Custom cursor color in SearchView), but there is not a non-ABS style for searchAutoCompleteTextView, so could not try this. I also looked for a Java method to set the text cursor drawable, but could not find one - I am modifying other aspects of the SearchView in Java and would be able to do so with the cursor if there were a method available.
我已经定制了搜索查看pretty的广泛开展,但是这最后一个变化是保持它寻找正确的 - 光标是白色在白色背景上,所以它不容易看到。的事情,我可以尝试任何其他的想法?
I have customized the SearchView pretty extensively, but this one last change is keeping it from looking right - the cursor is white on a white background, so it is not easily visible. Any other ideas of things I can try?
推荐答案
根据该意见,并回答上面我做了这到底是怎么看使用反射的例子。这解决了我的应用程序的问题。希望这样可以节省别人一些时间。
Based on the comments and answers above I made an example of how this could look using reflection. This solves the problem in my app. Hope it saves someone else some time.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.entity_list_actions, menu);
final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
final int textViewID = searchView.getContext().getResources().getIdentifier("android:id/search_src_text",null, null);
final AutoCompleteTextView searchTextView = (AutoCompleteTextView) searchView.findViewById(textViewID);
try {
Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
mCursorDrawableRes.setAccessible(true);
mCursorDrawableRes.set(searchTextView, 0); //This sets the cursor resource ID to 0 or @null which will make it visible on white background
} catch (Exception e) {}
return super.onCreateOptionsMenu(menu);
}
这是0也能像R.drawable.my_cursor任何其他资源ID
That 0 could be any other resource ID like R.drawable.my_cursor
这篇关于改变光标颜色搜索查看无ActionBarSherlock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!