中心搜索查看的操作栏 [英] Center SearchView in Action Bar
问题描述
我如何居中而ActionView(尤其是搜索查看)一个操作栏里面?
How can I center an ActionView (a SearchView in particular) inside of a Action Bar?
正如看到的谷歌图书应用:
As seen in the Google Books app:
  
我目前的布局设置的(<$ C C $> search_layout.xml )的:
My current layout setup (search_layout.xml
):
<?xml version="1.0" encoding="utf-8"?>
<SearchView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:queryHint="@string/search_hint" />
我的行动吧XML文件的(<$ C C $> menu.xml文件)的:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:icon="@+android:drawable/ic_menu_search"
android:title="@string/search"
android:showAsAction="always|collapseActionView"
android:id="@+id/searchMenuItem"
android:actionLayout="@layout/search_layout" />
</menu>
有没有办法模仿的图书搜索查看的行为?
Is there a way to mimic the behaviour of the Books' SearchView?
推荐答案
A 查看
为中心的动作条
即可'吨,在 XML菜单
,据我可以告诉。然而,它可以通过设置自定义布局的行动项目
实现动作条
实施。一个基本的例子:
A View
centered in the ActionBar
can't be achieved with Action Items
in an xml menu
, as far as I can tell. However it can be implemented by setting a custom layout to the ActionBar
. A basic example:
这是活动
:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setCustomView(R.layout.actionbar_layout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
其中, actionbar_layout
是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SearchView
android:id="@+id/mySearchView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:iconifiedByDefault="false" />
</RelativeLayout>
在7''平板电脑创建这样的:
creates this on a 7'' tablet:
请注意设置为假
在搜索查看$ C $的
iconifiedByDefault
属性C>迫使它出现膨胀,而不仅仅是为图标。这可能需要您隐藏softkeyboard编程或将被推出时打开活动开始。
Note the iconifiedByDefault
attribute set to false
on the SearchView
which forces it to appear expanded and not just as an icon. This may require that you hide the softkeyboard programmatically or it will be launched open when the Activity starts.
这篇关于中心搜索查看的操作栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!