搜索栏宽度很小

时间:2018-07-11 18:55:48

标签: java android search android-edittext android-custom-view

我在android应用中使用了自定义搜索栏。问题是,尽管操作栏中有多余的空间,但当搜索栏处于焦点位置时,其宽度却很小(如wrap_content)。但是当焦点移开时,将显示全文。在触摸输入的文本(短或长)时,也会出现软键盘。当文本较短时,触摸文本右侧剩余的多余空间不会出现键盘,但是在文本较长时会出现。 我希望EditText(搜索栏)的宽度尽可能长,例如match_parent,因为有足够的空间。有什么建议吗? Short text Long text

这是search_bar.xml

<EditText
    android:paddingStart="10dp"
    android:paddingEnd="10dp"
    android:id="@+id/et_search"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:imeOptions="actionSearch"
    android:hint="@string/hint"
    android:background="@android:color/transparent"/>

这是my_menu.xml

<item
    android:id="@+id/action_search"
    android:title="@string/search"
    app:showAsAction="ifRoom"
    android:icon="@drawable/ic_search">
</item>
<item
    android:id="@+id/share"
    android:title="@string/share_this_app"
    app:showAsAction="never"
    android:icon="@drawable/ic_share">
</item>
<item
    android:id="@+id/rate"
    android:title="@string/rate_this_app"
    app:showAsAction="never"
    android:icon="@drawable/ic_rate">
</item>
<item
    android:id="@+id/about"
    android:title="@string/about"
    app:showAsAction="never"
    android:icon="@drawable/ic_about">
</item>
<item
    android:id="@+id/more_apps"
    android:title="@string/more_apps"
    app:showAsAction="ifRoom"
    android:icon="@drawable/ic_more_apps">
</item>

这是onCreateOptionMenu()方法

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.my_menu, menu);
    MenuItem item = menu.findItem(R.id.more_apps);
    item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
    return true;
}

这是handleSearch()方法

private void handleMenuSearch() {
    ActionBar actionBar = getSupportActionBar(); //get the actionbar

    if (isSearchOpened) { //test if the search is open
        if (actionBar != null) {
            if (et_search.getText().toString().length() > 0) {
                et_search.getText().clear();
                doSearch(et_search.getText().toString()); //Clears the previous highlights
                //open the keyboard focused in the et_Search
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.showSoftInput(et_search, InputMethodManager.SHOW_IMPLICIT);
                }
            } else {
                et_search.getText().clear();
                doSearch(et_search.getText().toString()); //Clears the previous highlights
                actionBar.setDisplayShowCustomEnabled(false); //disable a custom view inside the actionbar
                actionBar.setDisplayShowTitleEnabled(true); //show the title in the actionbar

                //hides the keyboard
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(et_search.getWindowToken(), 0);
                }

                //add the search icon in the actionbar
                mSearchAction.setIcon(getResources().getDrawable(R.drawable.ic_open_search));

                isSearchOpened = false;
            }
        }
    } else { //open the search entry
        if (actionBar != null) {
            actionBar.setDisplayShowCustomEnabled(true); //enable it to display a custom view in the
            //action bar
            actionBar.setCustomView(R.layout.search_bar); //add the custom view
            actionBar.setDisplayShowTitleEnabled(false); //hide the title

            et_search = actionBar.getCustomView().findViewById(R.id.et_search); //the text editor
        }

        //this is a listener to do a search when the user clicks on search button
        et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    doSearch(et_search.getText().toString());
                    //hides the keyboard
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm != null) {
                        imm.hideSoftInputFromWindow(et_search.getWindowToken(), 0);
                    }
                    return true;
                }
                return false;
            }
        });

        et_search.requestFocus();

        //open the keyboard focused in the et_Search
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.showSoftInput(et_search, InputMethodManager.SHOW_IMPLICIT);
        }

        //add the close icon
        mSearchAction.setIcon(getResources().getDrawable(R.drawable.ic_close_search));

        isSearchOpened = true;
    }
}

1 个答案:

答案 0 :(得分:0)

尝试一下-

if (isSearchOpened) { //test if the search is open
        if (actionBar != null) {
            actionBar.setDisplayShowCustomEnabled(false); //disable a custom view inside the actionbar
            actionBar.setDisplayShowTitleEnabled(true); //show the title in the action bar
            // your other codes
        }
}

我的意思是在isSearchOpened==trueactionbar!=null时使用它,而不是仅在else条件下使用