搜索查看在OptionsMenu不是全宽 [英] SearchView in OptionsMenu not full width

查看:133
本文介绍了搜索查看在OptionsMenu不是全宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作搜索查看它扩展在我OptionsMenu当用户点击搜索图标。然而,它不仅扩展了其他OptionsMenu图标中的可用空间内。在宽屏幕,这是好的,但有一个狭窄的空间,只有房间显示5-10字数在搜索框中。我想等它为Android的联系人应用程序它覆盖其他图标。目前,我建设有targetSdkVersion = 17。希望我失去了一些东西简单:)

(注后补充:我已经找到了可行迄今为止唯一的解决办法是隐藏所有的菜单图标时,我想扩大搜索图标,这在概念上简单,但它是凌乱的,因为恢复时隐时现。图标,一个人要经过一堆逻辑来弄清楚恢复哪些,或围绕保持状态变量,等等。)

下面是在为OptionsMenu我的项目的xml:

 <项目
  机器人:ID =@ + ID / menu_search_shallow
  机器人:标题=搜索当前文件夹
  机器人:图标=@可绘制/ ic_btn_search
  机器人:showAsAction =永远| collapseActionView
  机器人:actionViewClass =android.widget.SearchView/>
 

我也有我的主要活动code:

  @覆盖
公共布尔onCreateOptionsMenu(菜单菜单)
{
  。getMenuInflater()膨胀(R.menu.nav_menu,菜单);
  this.optionsMenu =菜单;

  菜单项searchItem = menu.findItem(R.id.menu_search_shallow);
  sea​​rchItem.setOnActionExpandListener(本);
  搜索查看搜索查看=(搜索查看)searchItem.getActionView();
  sea​​rchView.setQueryHint(的getString(R.string.search_shallow_hint));

  sea​​rchItem = menu.findItem(R.id.menu_search_deep);
  sea​​rchItem.setOnActionExpandListener(本);
  搜索查看=(搜索查看)searchItem.getActionView();
  sea​​rchView.setQueryHint(的getString(R.string.search_deep_hint));
}
 

  @覆盖
公共布尔onMenuItemActionExpand(菜单项项)
{
  搜索查看搜索查看=(搜索查看)item.getActionView();
  sea​​rchView.setOnQueryTextListener(本);
  返回true;
}

@覆盖
公共布尔onMenuItemActionCollapse(菜单项项)
{
  搜索查看搜索查看=(搜索查看)item.getActionView();
  sea​​rchView.setQuery(,假);
  返回true;
}
 

解决方案

我也有类似的问题,即在搜索栏没有填写的整个宽度,(但我没有其他的图标)。 我在项目中加入解决了这个问题:

 安卓actionLayout =@布局/ my_search_view
 

和布局/ my_search_view.xml:

 < XML版本=1.0编码=UTF-8&GT?;
<搜索查看的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT/>
 

I have a working SearchView which expands in my OptionsMenu when the user taps on the search icon. However it only expands within the available space among the other OptionsMenu icons. On a wide screen this is fine, but with a narrow space there is only room to show 5-10 charaters in the search box. I want it to overlay the other icons such as it does for the Android Contacts app. Currently, I'm building with targetSdkVersion = 17. Hopefully I'm missing something simple :)

(Note added later: the only solution I've found workable so far is to hide all the menu icons when I want to expand the search icon. This is conceptually simple. But it is messy because when restoring hidden icons, one has to go through a bunch of logic to figure out which ones to restore, or keep state variables around, etc.)

Here's my item xml in for the OptionsMenu:

<item
  android:id="@+id/menu_search_shallow"
  android:title="Search Current Folder"
  android:icon="@drawable/ic_btn_search"
  android:showAsAction="always|collapseActionView"
  android:actionViewClass="android.widget.SearchView" />

I also have in my main activity code:

@Override
public boolean onCreateOptionsMenu (Menu menu)
{
  getMenuInflater().inflate(R.menu.nav_menu, menu);
  this.optionsMenu = menu;

  MenuItem searchItem = menu.findItem (R.id.menu_search_shallow);
  searchItem.setOnActionExpandListener (this);
  SearchView searchView = (SearchView) searchItem.getActionView();
  searchView.setQueryHint (getString (R.string.search_shallow_hint));

  searchItem = menu.findItem (R.id.menu_search_deep);
  searchItem.setOnActionExpandListener (this);
  searchView = (SearchView) searchItem.getActionView();
  searchView.setQueryHint (getString (R.string.search_deep_hint));
}

and

@Override
public boolean onMenuItemActionExpand(MenuItem item) 
{
  SearchView searchView = (SearchView) item.getActionView();
  searchView.setOnQueryTextListener (this);
  return true;
}

@Override
public boolean onMenuItemActionCollapse(MenuItem item) 
{
  SearchView searchView = (SearchView) item.getActionView();
  searchView.setQuery ("", false);
  return true;
}

解决方案

I had a similar issue, that the search bar did not fill the whole width,( but I had no other icons). I solved it by adding in item:

android:actionLayout="@layout/my_search_view"

and in layout/my_search_view.xml :

<?xml version="1.0" encoding="utf-8"?>
<SearchView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

这篇关于搜索查看在OptionsMenu不是全宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆