更改 searchView 的光标颜色
Change cursor color of searchView
我知道这个问题已经发布了数百万次,但我无法为我的案例找到有效的解决方案。
我需要更改 searchView 的光标颜色。
我没有 searchView 的 xml 定义,我以编程方式使用它。
这是我的代码:
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()));
return true;
}
我正在使用这个:
import android.support.v7.widget.SearchView;
谢谢
你尝试了吗?
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>
例如:Changing the cursor color in SearchView without ActionBarSherlock
仅在 onCreate()
方法中添加此代码对我有效
searchView = (SearchView)findViewById(R.id.search_view);
SearchView.SearchAutoComplete searchAutoComplete =
(SearchView.SearchAutoComplete)searchView.findViewById(androidx.appcompat.R.id.search_src_text);
AutoCompleteTextView searchTextView = (AutoCompleteTextView) searchView.findViewById(androidx.appcompat.R.id.search_src_text);
try {
Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
mCursorDrawableRes.setAccessible(true);
mCursorDrawableRes.set(searchTextView, 0);
} catch (Exception e) {
}
另一种改变光标颜色的方法可以通过样式来完成。
拳头创建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" /> //your desired cursor color
<size android:width="2dp" />
</shape>
在style/themeres文件夹中定义样式
<style name="cursorColor" parent="Widget.AppCompat.AutoCompleteTextView">
<item name="android:textCursorDrawable">@drawable/cursor</item>
并在您的相关 Activity 样式或 AppTheme
中添加以下行
<item name="autoCompleteTextViewStyle">@style/cursorColor</item>
我知道这个问题已经发布了数百万次,但我无法为我的案例找到有效的解决方案。
我需要更改 searchView 的光标颜色。 我没有 searchView 的 xml 定义,我以编程方式使用它。
这是我的代码:
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()));
return true;
}
我正在使用这个:
import android.support.v7.widget.SearchView;
谢谢
你尝试了吗?
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>
例如:Changing the cursor color in SearchView without ActionBarSherlock
仅在 onCreate()
方法中添加此代码对我有效
searchView = (SearchView)findViewById(R.id.search_view);
SearchView.SearchAutoComplete searchAutoComplete =
(SearchView.SearchAutoComplete)searchView.findViewById(androidx.appcompat.R.id.search_src_text);
AutoCompleteTextView searchTextView = (AutoCompleteTextView) searchView.findViewById(androidx.appcompat.R.id.search_src_text);
try {
Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
mCursorDrawableRes.setAccessible(true);
mCursorDrawableRes.set(searchTextView, 0);
} catch (Exception e) {
}
另一种改变光标颜色的方法可以通过样式来完成。
拳头创建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" /> //your desired cursor color
<size android:width="2dp" />
</shape>
在style/themeres文件夹中定义样式
<style name="cursorColor" parent="Widget.AppCompat.AutoCompleteTextView">
<item name="android:textCursorDrawable">@drawable/cursor</item>
并在您的相关 Activity 样式或 AppTheme
中添加以下行 <item name="autoCompleteTextViewStyle">@style/cursorColor</item>