以编程方式在SearchView中移动ImageView

时间:2017-02-07 19:29:07

标签: java android xml

发生了什么:

这是搜索前的屏幕

Before Search

这是屏幕上的搜索

After Search

问题:

一旦用户开始在SearchView栏中输入,X按钮就会向左拉。

我的尝试:

原始代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Create search bar on ActionBar
    getMenuInflater().inflate(R.menu.menu_schedule_search, menu);
    final MenuItem searchItem = menu.findItem(R.id.action_search);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setQueryHint(getString(R.string.search_hint));

    // Filter items with search keyword
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            // Reset SearchView
            searchView.clearFocus();
            searchView.setQuery("", false);
            searchView.setIconified(true);
            searchItem.collapseActionView();
            // Set activity title to search query
            ScheduleActivity.this.setTitle(query);
            mAdapter.filter(query, mSchedules);
            return true;
        }

        @Override
        public boolean onQueryTextChange(String searchKeyword) {
            mAdapter.filter(searchKeyword, mSchedules);
            return false;
        }
    });



    return super.onCreateOptionsMenu(menu);
}

我想在初始化setOnQueryTextListener

之前附加下面的代码段
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(android.app.ActionBar.LayoutParams.WRAP_CONTENT, android.app.ActionBar.LayoutParams.FILL_PARENT);
params.weight = 1.0f;
params.gravity = Gravity.RIGHT;

int closeButtonId = getResources().getIdentifier("android:id/search_close_btn", null, null);
ImageView closeButtonImage = (ImageView) searchView.findViewById(closeButtonId);
closeButtonImage.setLayoutParams(params);

但是,我知道这不会起作用,因为我的Activity是一个ListItem布局。事实上它崩溃了:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference at com.leoybkim.watexam.ScheduleActivity.onCreateOptionsMenu(ScheduleActivity.java:91)

我也不确定android:id/search_close_btn是否是正确的ID。它目前没有在我的xml中声明,但它似乎是在默认的Android库的运行时(?)上创建的。无法确定ImageView的来源可能是我最大的问题。 下面是我的搜索菜单的xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_search"
        android:title="@string/search_hint"
        android:icon="@drawable/ic_search_black_24dp"
        app:showAsAction="ifRoom|collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

1 个答案:

答案 0 :(得分:0)

这是我对搜索视图的代码片段,我认为这些代码没有您的问题。

    // Associate searchable configuration with the SearchView
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    MenuItem item = menu.findItem(R.id.search);
    SearchView searchView = (SearchView) item.getActionView();

    MenuItemCompat.setOnActionExpandListener(item, new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            mIngredientPresenter.queryTextChange(RxSearchView.queryTextChanges((SearchView) item.getActionView()));
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            mIngredientPresenter.clearQueryTextChange();
            return true;
        }
    });

    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

这是我的Xml文件。

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/search"
        android:icon="@drawable/search_icon"
        android:title="@string/search_title"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="collapseActionView|ifRoom"/>
</menu>