I want to display a fully expanded SearchView for particular activities in my application, however, the SearchView only spans half the ActionBar for these activities.
I'm currently using the v7 support library.
Here's the code for my SearchView in menu_home.xml:
<menu...
<item
android:id="@+id/action_search"
android:title="search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView" /
</menu>
Here's my Activity code:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
if (showSearch) {
getSupportActionBar().setDisplayShowTitleEnabled(false);
searchView.setIconifiedByDefault(false);
} else {
item.setVisible(false);
getSupportActionBar().setDisplayShowTitleEnabled(true);
}
return true;
}
And here's what the SearchView looks like:
I've tried setting the SearchView LayoutParams.width to LayoutParams.MATCH_PARENT, but SearchView.getLayoutParams() kept returning null. I tried ViewGroup.LayoutParams, LinearLayout.LayoutParams, ActionBar.LayoutParams, and v7.ActionBar.LayoutParams.
If it's any help, this activity is exclusively in Portrait mode.
MenuItemCompat
's SearchView
has a property named maxWidth
.
final MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setMaxWidth(xxx);
Use screen width instead of xxx
of course
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.