我想在searchview小部件中添加下划线,如下面给定图片中的蓝色下划线。但我的searchview不显示任何下划线。即使我输入文字也没有显示下划线。下面是我正在使用的代码
menu_home.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.peoplecloud.app.guggu.activities.HomeActivity">
<item
android:id="@+id/menu_item_search"
android:icon="@drawable/icon_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"
>
</item>
</menu>
代码 -
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.clear();
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_home, menu);
searchItem = menu.findItem(R.id.menu_item_search);
searchItem.setVisible(false);
mSearchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.menu_item_search));
mSearchView.setQueryHint("Search GIFs");
setupSearchView(searchItem);
return super.onCreateOptionsMenu(menu);
}
private void setupSearchView(MenuItem searchItem) {
searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
mSearchView.setQuery("test", true);
mSearchView.setOnQueryTextListener(this);
mSearchView.setFocusable(true);
mSearchView.requestFocusFromTouch();
mSearchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus)
{
if (hasFocus) {
showInputMethod(view.findFocus());
}
}
});
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s)
{
...
return true;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
}
});
}
答案 0 :(得分:0)
尝试以下操作,此处添加了自定义搜索视图布局。
<强> menu_home.xml 强>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.peoplecloud.app.guggu.activities.HomeActivity">
<item
android:id="@+id/menu_item_search"
android:icon="@drawable/icon_search"
app:actionLayout="@layout/search_layout"
app:showAsAction="always">
</item>
</menu>
<强> search_layout.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<EditText
android:id="@+id/txt_search"
android:textSize="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Search..."
android:inputType="text"
android:singleLine="true" />
</RelativeLayout>
<强> onCreateOptionsMenu 强>
/** Get the action view of the menu item whose id is search */
View v = (View) menu.findItem(R.id. menu_item_search).getActionView();
/** Get the edit text from the action view and add a text changed listener i.e a TextWatcher.
Here textWatcher is a TextWatcher object*/
txtSearch = (EditText) v.findViewById(R.id.txt_search);
txtSearch.addTextChangedListener(textWatcher);