如何保持展开的搜索查看右侧动作条的(同时使用自定义图标)? [英] How to keep expanded SearchView on the right side of ActionBar (while using custom icon)?
问题描述
我的问题是密切相关的搜索查看错误定位和<一href="http://stackoverflow.com/questions/19316113/searchview-positions-when-expanded-in-actionbar">SearchView当动作条膨胀位置,但答案贴有不工作。
&LT;项目机器人:ID =@ + ID / ACTION_SEARCH
机器人:图标=@可绘制/ navi_search
机器人:标题=@字符串/ ACTION_SEARCH
机器人:showAsAction =ifRoom | collapseActionView
机器人:actionViewClass =android.widget.SearchView
/&GT;
它看起来像这样在默认情况下(右上角,旁边溢出菜单):
但问题是,扩张时的我想要搜索查看留在右侧,但相反,它移动到左边,旁边的home键(应用程序图标)。
我试图改变 showAsAction
到这些(的这里建议一>):
安卓showAsAction =ifRoom
机器人:showAsAction =总是
...这确实解决对准膨胀的时候,却自定义图标丢失当折叠状态:
此外,这样的东西安卓layout_alignParentRight =真正的
和安卓layout_centerInParent =真正的
的菜单&LT;项目&GT;
没有任何效果
任何想法?
我打靶的Android 4.0及以上:
&LT;使用-SDK安卓的minSdkVersion =14机器人:targetSdkVersion =19/&GT;
好吧,我找到了一个方法:
在 onCreateOptionsMenu()
,标准菜单膨胀的东西后,设置
ActionBar.LayoutParams
与 Gravity.RIGHT
为搜索查看
例如:
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
MenuInflater充气= getMenuInflater();
inflater.inflate(R.menu.main_activity_actions,菜单);
// ...
//查找搜索查看;使其靠右侧,也扩展模式:
菜单项项= menu.findItem(R.id.action_search);
搜索查看搜索=(搜索查看)item.getActionView();
search.setLayoutParams(新ActionBar.LayoutParams(Gravity.RIGHT));
}
(在菜单定义XML,我仍然可以使用机器人:showAsAction =ifRoom | collapseActionView
)
现在的搜索查看开在右边,在同一个地方的图标,并自定义图标仍然在使用。
My question is closely related to SearchView wrong alignment and SearchView positions when expanded in ActionBar but the answers posted there do not work.
So, I'm using a SearchView in the Action Bar:
<item android:id="@+id/action_search"
android:icon="@drawable/navi_search"
android:title="@string/action_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.widget.SearchView"
/>
It looks like this by default (top-right corner, next to overflow menu):
Problem is, when expanded, I'd like the SearchView to stay on the right, but instead, it moves to the left edge, next to home button (app icon).
I tried changing showAsAction
to either of these (as suggested here):
android:showAsAction="ifRoom"
android:showAsAction="always"
...And that does fix the alignment when expanded, but the custom icon is lost when in collapsed state:
Also, stuff like android:layout_alignParentRight="true"
and android:layout_centerInParent="true"
on the menu <item>
have no effect.
Any ideas?
I'm targetting Android 4.0 and above:
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" />
Alright, I found a way:
In onCreateOptionsMenu()
, after the standard menu inflating stuff, set
ActionBar.LayoutParams
with Gravity.RIGHT
for the SearchView
instance:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
// ...
// Find the SearchView; make it aligned to right, also in expanded mode:
MenuItem item = menu.findItem(R.id.action_search);
SearchView search = (SearchView) item.getActionView();
search.setLayoutParams(new ActionBar.LayoutParams(Gravity.RIGHT));
}
(In the menu definition XML, I still use android:showAsAction="ifRoom|collapseActionView"
.)
Now the SearchView opens on the right, in the same place where the icon is, and the custom icon is still in use.
这篇关于如何保持展开的搜索查看右侧动作条的(同时使用自定义图标)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!