metLoginUserName=(EditText)this.findViewById(R.id.etLoginUserName);
metLoginPassword=(EditText)this.findViewById(R.id.etLoginPassword);
/**Set the hint in username and password edittext*/
metLoginUserName=HotSpotStaticMethod.setHintEditText(metLoginUserName, getString(R.string.hint_username),true);
metLoginPassword=HotSpotStaticMethod.setHintEditText(metLoginPassword, getString(R.string.hint_password),true);
SearchView对象从 LinearLayout扩展,因此它保存其他视图。诀窍是找到包含提示文本的视图,并通过编程方式更改颜色。通过 id 查找视图的问题在于 id 依赖于应用程序中使用的主题。因此,根据所使用的主题,findViewById(int id)方法可能返回 null。适用于每个主题的更好方法是遍历视图层次结构并找到包含提示文本的小部件:
// 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);
使用这种方法,您还可以更改 SearchView层次结构中其他小部件的外观,例如保存搜索查询的 EditText。除非 Google 决定在短时间内更改 SearchView的视图层次结构,否则您应该能够使用此方法在一段时间内更改小部件的外观。
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.custommenu, menu);
}
val searchView = SearchView(this)
val editText = searchView.findViewById<EditText>(androidx.appcompat.R.id.search_src_text)
editText.setTextColor(Color.WHITE)
searchView = view.findViewById(R.id.searchView_Contacts)
// change color
val id = searchView.context.resources
.getIdentifier("android:id/search_src_text", null, null)
val textView = searchView.findViewById<View>(id) as TextView
textView.setTextColor(Color.WHITE)
<style name="ThemeOverlay.Toobar" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
<!-- Text color -->
<item name="android:editTextColor">@color/....</item>
<!-- Hint text color -->
<item name="android:textColorHint">@color/...</item>
</style>
AppCompat 主题:
<style name="ThemeOverlay.Toobar" parent="ThemeOverlay.AppCompat.Light.*">
<!-- Text color -->
<item name="android:editTextColor">@color/....</item>
<!-- Hint text color -->
<item name="android:textColorHint">@color/...</item>
</style>
<style name="ThemeOverlay.SearchView" parent="">
<!-- Text color -->
<item name="android:editTextColor">@color/...</item>
<!-- Hint text color -->
<item name="android:textColorHint">@color/...</item>
</style>