设置Actionbar SearchItem的背景

时间:2013-10-17 03:52:32

标签: android android-layout android-actionbar

我想将背景(类似EditText背景)设置为我的ActionBar SearchItem。这是当前的行动吧。

enter image description here

我想将此背景设为可绘制的。

enter image description here

以下是我创建searchItem

的方法
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.guest_list, menu);

    MenuItem searchItem = menu.findItem(R.id.search);
    SearchManager searchManager = (SearchManager) getActivity()
            .getSystemService(Context.SEARCH_SERVICE);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        SearchView searchView = (SearchView) searchItem.getActionView();
        searchView.setSearchableInfo(searchManager
                .getSearchableInfo(getActivity().getComponentName()));
        searchView.setIconified(true);
        searchView.setSubmitButtonEnabled(true);
        searchView.setOnQueryTextListener(new OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String query) {
                setFilter(query);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String query) {
                setFilter(query);
                return false;
            }
        });
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        SearchView searchView = (SearchView) searchItem.getActionView();

        searchView.setSearchableInfo(searchManager
                .getSearchableInfo(getActivity().getComponentName()));
        searchView.setIconified(true);
        searchView.setSubmitButtonEnabled(true);
        searchView.setOnQueryTextListener(new OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String query) {
                setFilter(query);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String query) {
                setFilter(query);
                return false;
            }
        });
    }
}

我已尝试按searchView.setBackgroundDrawable设置背景,但我没有得到我想要实施的内容。

1 个答案:

答案 0 :(得分:1)

请查看以下链接,因为它解释了操作栏中搜索栏的所有内容:

Changing the background drawable of the searchview widget