问题描述
如何建立动作条
与单一的操作项可见,可折叠的搜索视图时,搜索视图展开?为了更好的描述,这正是我需要的:
请注意,有其他菜单项和机器人:uiOptions =splitActionBarWhenNarrow
在的AndroidManifest.xml
定义
我试图建立自定义搜索项目的布局:
<项目 机器人:ID =@ + ID / menu_search 机器人:actionLayout =@布局/ actionbar_search 机器人:图标=@可绘制/ ACTION_SEARCH 机器人:showAsAction =永远| collapseActionView 机器人:标题=@字符串/搜索/>
< XML版本=1.0编码=UTF-8&GT?;< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android 机器人:layout_width =match_parent 机器人:layout_height =match_parent 机器人:重力=右 机器人:方向=横向> < com.actionbarsherlock.widget.SearchView 机器人:ID =@ + ID / search_view 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =match_parent 机器人:iconifiedByDefault =假 机器人:queryHint =@字符串/ search_hint/> <的ImageButton 机器人:ID =@ + ID /方法add_item 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =match_parent 风格=@风格/ Widget.Sherlock.ActionButton 机器人:SRC =@可绘制/ content_new/>< / LinearLayout中>
不过,在默认情况下搜索视图将所有可用的宽度和按键是不可见的。我不知道该怎么给力搜索查看
来填补应用程序图标和菜单项之间的所有可用空间。我所发现的是安卓了maxWidth
属性,但这种只允许硬codeD方面,我正在寻找一些更灵活的解决方案。我也尝试 RelativeLayout的
与机器人:layout_toRightOf =@ ID / search_view
,没有运气
考虑从意见建议后,许多谷歌搜索和测试成功地获得了预期的效果。我不是这个解决方案而感到自豪,但它的工作原理,这不是太复杂,应该够这种情况下。
在简历我做了什么:
- 放在
搜索查看
自定义的customView
动作条的
添加项按钮code>。 - 删除
collapseActionView
和安卓actionLayout
从搜索项menu.xml文件
- 折叠/展开
搜索查看
编程。
有些code,以更好地理解我所做的。也许这可能是有用的人。
<项目 机器人:ID =@ + ID / menu_search 机器人:图标=@可绘制/ ACTION_SEARCH 机器人:showAsAction =总是 机器人:标题=@字符串/搜索/>// ...其他菜单项
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android 机器人:layout_width =match_parent 机器人:layout_height =match_parent 机器人:重力=右 机器人:方向=横向> < com.actionbarsherlock.widget.SearchView 机器人:ID =@ + ID / search_view 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =match_parent 机器人:iconifiedByDefault =假 机器人:queryHint =@字符串/ search_hint 机器人:能见度=隐形/> <的ImageButton 机器人:ID =@ + ID /方法add_item 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =match_parent 风格=@风格/ Widget.Sherlock.ActionButton 机器人:SRC =@可绘制/ content_new 机器人:标题=@字符串/方法add_item/>< / LinearLayout中>
@覆盖公共无效的onCreate(包savedInstanceState){ // ... actionBar.setCustomView(R.layout.actionbar_search); actionBarCustomView = ab.getCustomView(); 搜索查看=((搜索查看)actionBarCustomView.findViewById(R.id.search_view)); searchView.setOnCloseListener(新SearchView.OnCloseListener(){ @覆盖 公共布尔的OnClose(){ searchView.setVisibility(View.INVISIBLE); 返回false; } });}@覆盖公共布尔onOptionsItemSelected(菜单项项){ 开关(item.getItemId()){ // ... 案例R.id.menu_search: 如果(searchView.getVisibility()== View.VISIBLE){ searchView.setIconified(真正的); searchView.setVisibility(View.INVISIBLE); } 其他 { searchView.setMaxWidth(abCustomView.getWidth() - addButton.getWidth()); searchView.setVisibility(View.VISIBLE); searchView.setIconified(假); } 打破; // ... } 返回true;}@覆盖公共无效onBack pressed(){ 如果(searchView.getVisibility()== View.VISIBLE){ searchView.setIconified(真正的); searchView.setVisibility(View.INVISIBLE); 返回; } // ...}
How to build ActionBar
with collapsible search view with single action item visible, when search view is expanded? To be more descriptive, this is what I need:
Note that there are other menu items and android:uiOptions="splitActionBarWhenNarrow"
is defined in AndroidManifest.xml
.
I tried to set up custom search item layout:
<item
android:id="@+id/menu_search"
android:actionLayout="@layout/actionbar_search"
android:icon="@drawable/action_search"
android:showAsAction="always|collapseActionView"
android:title="@string/search" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="horizontal" >
<com.actionbarsherlock.widget.SearchView
android:id="@+id/search_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:iconifiedByDefault="false"
android:queryHint="@string/search_hint" />
<ImageButton
android:id="@+id/add_item"
android:layout_width="wrap_content"
android:layout_height="match_parent"
style="@style/Widget.Sherlock.ActionButton"
android:src="@drawable/content_new"/>
</LinearLayout>
But by default search view takes all available width and button is not visible. I don't know how to force SearchView
to fill all available space between app icon and menu item. All I found is android:maxWidth
property, but this only allows to hardcoded dimension, and I'm looking for some more flexible solution. I tried also RelativeLayout
with android:layout_toRightOf="@id/search_view"
with no luck.
After considering the suggestions from comments, many googling and testing succeeded in obtaining the desired effect. I'm not proud of this solution but it works, it's not too complicated and should be enough for this case.
In resume what I did:
- Placed
SearchView
with custom "add item" button incustomView
ofActionBar
. - Removed
collapseActionView
andandroid:actionLayout
from search item inmenu.xml
- Collapsing/expanding
SearchView
programmatically.
Some code to better understand what I did. Maybe this can be useful for someone.
<item
android:id="@+id/menu_search"
android:icon="@drawable/action_search"
android:showAsAction="always"
android:title="@string/search" />
// ... other menu items
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="horizontal">
<com.actionbarsherlock.widget.SearchView
android:id="@+id/search_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:iconifiedByDefault="false"
android:queryHint="@string/search_hint"
android:visibility="invisible" />
<ImageButton
android:id="@+id/add_item"
android:layout_width="wrap_content"
android:layout_height="match_parent"
style="@style/Widget.Sherlock.ActionButton"
android:src="@drawable/content_new"
android:title="@string/add_item" />
</LinearLayout>
@Override
public void onCreate(Bundle savedInstanceState) {
// ...
actionBar.setCustomView(R.layout.actionbar_search);
actionBarCustomView = ab.getCustomView();
searchView = ((SearchView) actionBarCustomView.findViewById(R.id.search_view));
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
searchView.setVisibility(View.INVISIBLE);
return false;
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// ...
case R.id.menu_search:
if (searchView.getVisibility() == View.VISIBLE) {
searchView.setIconified(true);
searchView.setVisibility(View.INVISIBLE);
} else {
searchView.setMaxWidth(abCustomView.getWidth() - addButton.getWidth());
searchView.setVisibility(View.VISIBLE);
searchView.setIconified(false);
}
break;
// ...
}
return true;
}
@Override
public void onBackPressed() {
if (searchView.getVisibility() == View.VISIBLE) {
searchView.setIconified(true);
searchView.setVisibility(View.INVISIBLE);
return;
}
// ...
}
这篇关于Android的动作条:可折叠搜索查看与操作按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!