AutoCompleteTextView setDropDownWidth()不起作用

时间:2015-02-19 12:02:46

标签: android autocompletetextview

我已在SearchView中实施了ActionBar,其中我使用SimpleCursorAdaptersetSuggestionAdapter()设置了一些随机数据。

我遇到的问题是我无法让setDropDownWidth()处理SearchView的自定义AutoCompleteTextView。我可以更改背景资源和几乎所有内容,除了horizo​​ntaloffset和dropdownwidth。这是代码:

SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete) searchView.findViewById(R.id.search_src_text);
searchAutoComplete.setDropDownAnchor(R.id.anchorView);
searchAutoComplete.setDropDownWidth(LinearLayout.LayoutParams.MATCH_PARENT);
searchAutoComplete.setDropDownBackgroundResource(R.drawable.white_bg);
searchAutoComplete.setMinimumWidth(AppUtil.getDeviceWidth(activityContext));

请帮帮我!我一直在测试的设备是Nexus 4和Lollipop。

编辑:

我很困惑为什么setDropDownHeight()正在工作但是setDropDownWidth()无效,即使在设置int值时(Say 50)。下拉列表的宽度保持不变。我既不能减少也不能增加下拉列表的宽度。它是带有AppCompat主题的ActionBar中的错误吗?

编辑2:

我能够让方法setDropDownWidth()正常工作。我在Anchor View上使用了View.onLayoutChangeListener,在onLayout()内部方法中,我设置了下拉的宽度。它现在工作正常。我现在唯一的问题是为什么我们必须等待锚视图的布局更改监听器来设置下拉宽度?

1 个答案:

答案 0 :(得分:1)

来自文档

  

public void setDropDownWidth(int width)   在API级别3中添加   设置自动完成下拉列表的当前宽度。这可以是固定宽度,或填充屏幕的MATCH_PARENT,或WRAP_CONTENT以适合其锚视图的宽度

我遇到了同样的问题&我认为这是因为它改变了当前 withd所以每次调用 TextChangeListener 时都需要调用它。所以我把方法放在文本观察器&它运作得很好:

 auto.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub
    //    auto.setDropDownWidth(240);

    }

        @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub
             auto.setDropDownWidth(240);

    }