搜索视图不会扩展到工具栏中的父级

时间:2017-09-22 11:42:30

标签: java android android-layout button searchview

我正在将搜索功能添加到我的应用中。我将搜索小部件添加到工具栏上的片段中,但现在我希望它通过语音搜索扩展到最大值,并在搜索框中再添加一个按钮。

这是它的外观

  1. 片段开始时:使用切换按钮在工具栏上显示搜索图标。
  2. 点按搜索按钮时:SearchView展开但不完整展开为父
  3. 在键盘上按下提交时:隐藏按下搜索按钮时无法正常工作的开关按钮。仅在按下提交按钮时有效。
  4. 这是我的工具栏菜单(SearchView)代码

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();
        inflater.inflate(R.menu.searchmenu,menu);
    
    MenuItem item=menu.findItem(R.id.search_PTL);
    searchView=new SearchView(((Navigation_Drawer)getActivity()).getSupportActionBar().getThemedContext());
    MenuItemCompat.setShowAsAction(item,MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW|MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
    MenuItemCompat.setActionView(item,searchView);
    Button voiceSearch=new Button(getActivity());
    LinearLayout.LayoutParams navButtonsParams = new LinearLayout.LayoutParams(toolbar.getHeight() * 2 / 3, toolbar.getHeight() * 2 / 3);
    voiceSearch.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_access_time_black_24dp));
    ((LinearLayout)searchView.getChildAt(0)).addView(voiceSearch,navButtonsParams);
    ((LinearLayout)searchView.getChildAt(0)).setGravity(Gravity.CENTER_VERTICAL);
    // Toolbar.LayoutParams paramsSearch=new Toolbar.LayoutParams(Toolbar.LayoutParams.MATCH_PARENT,Toolbar.LayoutParams.MATCH_PARENT); 
    // searchView.setLayoutParams(paramsSearch);
    
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            switchButton.setVisibility(View.VISIBLE);
            Log.d(TAG,"Query Text Submit "+query);
            searchView.clearFocus();
            return false;
        }
    
        @Override
        public boolean onQueryTextChange(String newText) {
            Log.d(TAG,"Query Text Change "+newText);
    
            return false;
        }
    });
    searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            Log.d(TAG,"On Focus Change Listener "+b);
            switchButton.setVisibility(View.GONE);
            searchView.setQueryHint("Search ");
        }
    });
    searchView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG,"Search Button On Clicked Pressed");
              // THIS NEVER CALL ON PRESSING OR SUBMIT SEARCH BUTTON
    
        }
    });
    }
    

    searchmenu.xml

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <item android:id="@+id/search_PTL"
            android:title="Search"
            android:actionLayout="@layout/search_toolbar"
            android:icon="@drawable/button_ic_search"
            android:actionViewClass="android.widget.SearchView"
            app:showAsAction="ifRoom|collapseActionView">
        </item>
    
        <item android:id="@+id/toggle_Button"
            android:title=""
            android:visible="true"
            app:actionLayout="@layout/toggle_button"
            app:showAsAction="ifRoom">
    
        </item>
    
    </menu>
    

    search_toolbar布局

    <?xml version="1.0" encoding="utf-8"?>
    <SearchView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    
    </SearchView>
    

    我希望我的搜索视图

    1. 使用搜索框内的按钮展开到谷歌应用和gmail应用等家长。
    2. 点击搜索按钮后立即需要隐藏切换按钮。

1 个答案:

答案 0 :(得分:0)

使用此:

<item android:id="@+id/search_PTL"
      android:title="Search"
      app:actionLayout="@layout/search_toolbar"
      android:icon="@drawable/button_ic_search"
      app:actionViewClass="android.support.v7.widget.SearchView"
      app:showAsAction="ifRoom|collapseActionView">
</item>

而不是:

<item android:id="@+id/search_PTL"
        android:title="Search"
        android:actionLayout="@layout/search_toolbar"
        android:icon="@drawable/button_ic_search"
        android:actionViewClass="android.widget.SearchView"
        app:showAsAction="ifRoom|collapseActionView">
 </item>

请按照以下步骤操作。

  1. onCreateOptionsMenu

    中更改此内容
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.searchmenu, menu);
    
        final MenuItem item = menu.findItem(R.id.search_PTL);
        final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
        searchView.setOnQueryTextListener(this);
        return true;
    
    }
    
  2. 对您的活动实施SearchView.OnQueryTextListener