你好。我试图改变SearchView文本输入光标颜色,我面临很多困难。我发现它取决于我在AppTheme中定义的colorAccent值,但我无法改变它,因为它会影响很多其他UI元素。当我尝试使用自己的colorAccent参数将自定义样式设置到我的工具栏时,没有任何变化,光标仍然是相同的颜色(非常靠近工具栏,因此它似乎是不可见的)。
我的工具栏
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/newsfeed_toolbar"
android:layout_width="match_parent"
android:visibility="visible"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
style="@style/SearchStyle"
app:titleTextAppearance="@style/toolbar_title_style"
popupTheme="@style/AppTheme.PopupOverlay" >
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
我的搜索菜单
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/newsfeed_search"
android:icon="@mipmap/ic_search"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
</menu>
我的风格
<style name="SearchStyle" parent="@style/ThemeOverlay.AppCompat.ActionBar">
<item name="android:textCursorDrawable">@android:color/white</item>
<item name="android:colorAccent">@android:color/white</item>
</style>
活动代码
toolbar = (Toolbar) findViewById(R.id.newsfeed_toolbar);
setSupportActionBar(toolbar);
@Override
public boolean onCreateOptionsMenu( Menu menu) {
getMenuInflater().inflate( R.menu.menu, menu);
MenuItem searchItem = menu.findItem( R.id.newsfeed_search);
SearchView searchView = (SearchView) searchItem.getActionView();
\\The next few lines is what I tried from other StackOverFlow responses, but no success
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, android.R.color.white); //This sets the cursor resource ID to 0 or @null which will make it visible on white background
} catch (Exception e) {}
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
searchWord = query;
\\API Stuff
return false;
}
@Override
public boolean onQueryTextChange(String s) {
if (s.length()>5) {
\\API Stuff
return false;
}
});
MenuItemCompat.setOnActionExpandListener(searchItem,new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
searchMode = true;
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
searchMode = false;
searchWord = null;
pageCount = 1;
return true;
}
});
return true;
}
答案 0 :(得分:12)
我遇到同样的问题,但我终于通过风格
找到了将此项目样式添加到活动主题styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="autoCompleteTextViewStyle">@style/cursorColor</item>
</style>
<style name="cursorColor" parent="Widget.AppCompat.AutoCompleteTextView">
<item name="android:textCursorDrawable">@drawable/cursor</item>
</style>
然后在drawable文件夹中添加 cursor.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#FFFFFF" />
<size android:width="1dp" />
</shape>
删除搜索样式,无论您在工具栏中设置了什么。
答案 1 :(得分:0)
当我尝试使用自己的colorAccent参数将自定义样式设置为我的工具栏时,没有任何更改
当您将样式设置为工具栏时,我发现您有拼写错误
<android.support.v7.widget.Toolbar
android:id="@+id/newsfeed_toolbar"
android:layout_width="match_parent"
android:visibility="visible"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
style="@style/SearcStyle"
app:titleTextAppearance="@style/toolbar_title_style"
popupTheme="@style/AppTheme.PopupOverlay" >
SearcStyle
可能是这种情况吗?
答案 2 :(得分:0)
**Try this**
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_contacts, menu);
SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView search = (SearchView) menu.findItem(R.id.action_search).getActionView();
search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));
AutoCompleteTextView searchTextView = (AutoCompleteTextView) search.findViewById(android.support.v7.appcompat.R.id.search_src_text);
try {
Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
mCursorDrawableRes.setAccessible(true);
mCursorDrawableRes.set(searchTextView, R.drawable.cursor); //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);
}
**drawable/cursor.xml **
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ffffff" />
<size android:width="2dp" />
</shape>
答案 3 :(得分:0)
这是android的组件。实现不同。
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:autoCompleteTextViewStyle">@style/searchViwStyle</item>
</style>
<style name="searchViwStyle" parent="Widget.AppCompat.AutoCompleteTextView">
<item name="android:textCursorDrawable">@drawable/color_cursor</item>
</style>
和 drawable / color_cursor 。
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<size android:width="2dp" />
<solid android:color="@color/cursor_color" />
</shape>