问题
I have a problem with my searchview. I'm trying to make search suggestions full width but the suggestions have a right padding.
<-- language: lang-java -->
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
final MenuItem item=menu.findItem(R.id.procura);
MenuItemCompat.setOnActionExpandListener(item, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
tbs.setVisibility(View.GONE);
vwpgr.setVisibility(View.GONE);
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
tbs.setVisibility(View.VISIBLE);
vwpgr.setVisibility(View.VISIBLE);
return true;
}
});
searchView=(SearchView) MenuItemCompat.getActionView(item);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setQueryHint(getResources().getString(R.string.app_hint));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Intent search=new Intent(MainActivity.this,searchableactivity.class);
search.putExtra("searchquery",query);
startActivity(search);
searchView.clearFocus();
item.collapseActionView();
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
final AutoCompleteTextView searchTextView = (AutoCompleteTextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
//this remove left padding
searchTextView.setDropDownAnchor(R.id.appbar);
// this is not working
searchTextView.setDropDownWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
//and i tryed this too: searchTextView.setDropDownWidth(ViewGroup.LayoutParams.MATCH_PARENT);
try {
Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
mCursorDrawableRes.setAccessible(true);
mCursorDrawableRes.set(searchTextView, R.drawable.search_cursor); //This sets the cursor resource ID to 0 or @null which will make it visible on white background
} catch (Exception e) {
}
return true;
}
my menu 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=".MainActivity">
<item
android:id="@+id/procura"
android:title="@string/app_hint"
android:icon="@mipmap/ic_search_black_24dp"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
And I tried these Stack Overflow answers:
solution 1
solution 2
solution 3
How can I solve this?
回答1:
Try this
searchTextView.setDropDownWidth(ViewGroup.LayoutParams.MATCH_PARENT);
Instead of
searchTextView.setDropDownWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
回答2:
Here is my solution , By only setting the SearchView.SearchAutoComplete view DropDownWidth to match parent or width of the screen wont work because it is limited by the background resource which has padding on all sides .
So I understood that for getting it to work we must change also the background.
Solution : (this must be done each time view layout dimensions changes) :
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String query) {
SearchView.SearchAutoComplete mSearchSrcTextView = (SearchView.SearchAutoComplete) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
mSearchSrcTextView.setDropDownWidth(getResources().getDisplayMetrics().widthPixels);
mSearchSrcTextView.setDropDownBackgroundResource(R.color.colorGray);
modelManager.getLocationsSearchResults(query);
return true;
}
});
Result
来源:https://stackoverflow.com/questions/41107904/android-searchview-suggestions-padding-right