SearchView width + margin of AutoComplete dropdown

我的未来我决定 提交于 2019-12-08 04:55:34

问题


I'm trying to set the dropdown ListView generated by the AutoCompleteTextView to match_parent (full width of the screen)

This is what I got so far:

I'm also using a SearchView inspired by this answer Here

Here is the activity that inflates the menu

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_search, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    searchItem.expandActionView();

    MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            return false;
        }
        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            onBackPressed();
            return false;
        }
    });


    final ArrayAdapterSearchView searchView = (ArrayAdapterSearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setQueryHint(getResources().getString(R.string.location_autocomplete_hint));
    searchView.setAdapter(mPlaceAdapter);

    searchView.setDropDownAnchor(R.id.anchor_dropdown);
    searchView.setDropDownWidth(ViewGroup.LayoutParams.MATCH_PARENT);


    searchView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            searchView.setText(mPlaceAdapter.getItem(position).toString());
            startSearch(position);

        }
    });

The ArrayAdapter

public class ArrayAdapterSearchView extends SearchView {

    private SearchView.SearchAutoComplete mSearchAutoComplete;

    public ArrayAdapterSearchView(Context context) {
        super(context);
        initialize();
    }

    public ArrayAdapterSearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    public void initialize() {
        mSearchAutoComplete = (SearchAutoComplete)     findViewById(android.support.v7.appcompat.R.id.search_src_text);
        this.setAdapter(null);
        this.setOnItemClickListener(null);
    }

    @Override
    public void setSuggestionsAdapter(CursorAdapter adapter) {
    // don't let anyone touch this
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener listener) {
        mSearchAutoComplete.setOnItemClickListener(listener);
    }

    public void setAdapter(ArrayAdapter<?> adapter) {
        mSearchAutoComplete.setAdapter(adapter);
    }

    public void setText(String text) {
        mSearchAutoComplete.setText(text);
    }

    public void setDropDownVerticalOffset(int value) {
        mSearchAutoComplete.setDropDownVerticalOffset(value);
    }

    public void setDropDownAnchor(int value) {
        mSearchAutoComplete.setDropDownAnchor(value);
    }

    public void setDropDownWidth (int value) {
        mSearchAutoComplete.setDropDownWidth(value);
    }

    public void setDropDownBackgroundResource(Drawable value) {
        mSearchAutoComplete.setDropDownBackgroundDrawable(value);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mSearchAutoComplete.getLayoutParams();
        params.setMargins(0, 0, 0, 0); //substitute parameters for left, top, right, bottom
        mSearchAutoComplete.setLayoutParams(params);
    }

}

And finally the menu_search.xml file

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_margin="0dp"
    android:padding="0dp"
    tools:context="com.example.activities.SearchActivity">

    <item
        android:id="@+id/action_search"
        android:hint="@string/location_autocomplete_hint"
        android:title="@string/location_autocomplete_hint"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="collapseActionView"
        app:actionViewClass="com.example.adapters.ArrayAdapterSearchView" />

</menu>

The main activity xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/myWindowBackground"
    tools:context="com.example.activities.SearchActiviKty"
    android:orientation="vertical">

<include
    android:id="@+id/toolbar_actionbar"
    layout="@layout/toolbar_default"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<View
    android:id="@+id/anchor_dropdown"
    android:layout_width="fill_parent"
    android:layout_height="16dp"/>

</LinearLayout>

Finally, I'm using a simple TextView to populate the ListView.

My Thoughts

I think the SearchView has something to do with this issue, because If I replace the SearchView by a simple AutoCompleteTextView, the dropdown takes the entire width of the screen.

Thanks!


回答1:


My way is create another anchor view which has margin left and right, and it height is 1dp. setDropdownAnchor() to that view and drop down will match your view as you please



来源:https://stackoverflow.com/questions/32333551/searchview-width-margin-of-autocomplete-dropdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!