我们可以在android中使用工具栏以外的搜索小部件吗

我们可以在android中使用工具栏以外的搜索小部件吗,android,Android,我知道我们可以在活动工具栏中使用搜索小部件。根据我的要求,我们不能将工具栏用于搜索小部件,我们是否可以将搜索小部件作为单独的实体在工具栏下方的活动中或任何我们想要的地方(如文本视图或按钮)? 我已经创建了搜索视图xml,并通过包括我想要的任何位置来使用它 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/

我知道我们可以在活动工具栏中使用搜索小部件。根据我的要求,我们不能将工具栏用于搜索小部件,我们是否可以将搜索小部件作为单独的实体在工具栏下方的活动中或任何我们想要的地方(如文本视图或按钮)?

我已经创建了搜索视图xml,并通过包括我想要的任何位置来使用它

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:padding="16dp">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@android:color/white"
        app:cardElevation="4dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <android.support.v7.widget.SearchView
                android:id="@+id/searchView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:queryHint="Search..">

            </android.support.v7.widget.SearchView>

        </LinearLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

包括在布局中

<include layout="@layout/search_view" />

是的,这是可能的……像这样做

在XML中

<FrameLayout
    android:focusableInTouchMode="true"
    android:id="@+id/searchBoxFrame"
    android:layout_below="@+id/textViewTitle"
    android:layout_height="40dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="16dp"
    android:layout_width="fill_parent">

    <EditText
        android:background="@drawable/input_box"
        android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 "
        android:drawableLeft="@android:drawable/ic_menu_search"
        android:hint="Search"
        android:id="@+id/autoText"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:lines="1"
        android:paddingLeft="7dp"
        android:textSize="20sp" />

    <Button
        android:background="@android:drawable/ic_menu_close_clear_cancel"
        android:id="@+id/searchBoxCancel"
        android:layout_gravity="right|center_vertical"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:layout_width="40dp"
        android:visibility="gone" />
</FrameLayout>

在爪哇

private EditText searchEditText;
private Button cancelButton;

searchEditText = (EditText) findViewById(R.id.autoText);

cancelButton = (Button) findViewById(R.id.searchBoxCancel);

private void onEditTextItemChanged() {
    searchEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            list.clear();
            String editTextString = searchEditText.getText().toString();
            int textLength = editTextString.length();
            if (textLength > 0) {
                cancelButton.setVisibility(View.VISIBLE);
            } else {
                cancelButton.setVisibility(View.GONE);
            }
            for (int i = 0; i < list.size(); i++) {
                String searchText = "";
                searchText = list.get(i).getName().toString();
                if (textLength <= searchText.length()) {
                    // compare the String in EditText with Names in the
                    // ArrayList
                    if (searchText.toLowerCase().contains(editTextString.toLowerCase())) {
                        list.add(list.get(i));
                    }
                }
            }
            listItemsAdapter = new listItemsAdapter(getApplicationContext(), list);
            listView.setAdapter(listItemsAdapter);
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });

}

private void onCancelButtonClicked() {
    cancelButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            searchEditText.setText("");
        }
    });
}
private EditText search EditText;
私人按钮取消按钮;
searchEditText=(EditText)findViewById(R.id.autoText);
cancelButton=(按钮)findViewById(R.id.searchBoxCancel);
私有void onEditTextItemChanged(){
searchEditText.addTextChangedListener(新的TextWatcher(){
@凌驾
更改前文本之前的公共void(字符序列s、int start、int count、int after){
}
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
list.clear();
String editTextString=searchEditText.getText().toString();
int textLength=editTextString.length();
如果(文本长度>0){
取消按钮。设置可见性(视图。可见);
}否则{
cancelButton.setVisibility(View.GONE);
}
对于(int i=0;i如果(textLength)感谢您的回复。我正在尝试使用android:voiceSearchMode=“showVoiceSearchButton”添加语音搜索按钮,但它没有显示出来?对此有什么想法吗?我会去做的。完成后我会通知您。