中心搜索查看的操作栏 [英] Center SearchView in Action Bar

查看:131
本文介绍了中心搜索查看的操作栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何居中而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:

请注意设置为搜索查看 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屋!

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