【问题标题】:AutoCompleteTextView setDropDownWidth() not workingAutoCompleteTextView setDropDownWidth()不起作用
【发布时间】:2015-04-20 18:27:32
【问题描述】:

我在ActionBar 中实现了SearchView,其中我使用setSuggestionAdapter() 设置了带有一些随机数据的SimpleCursorAdapter

我面临的问题是我无法让setDropDownWidth() 处理SearchView 的自定义AutoCompleteTextView。我可以更改背景资源和几乎所有内容,除了水平偏移和下拉宽度。代码如下:

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));

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

编辑:

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

编辑2:

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

【问题讨论】:

    标签: android autocompletetextview


    【解决方案1】:

    来自文档

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

    我遇到了同样的问题,我认为这是因为它改变了 current 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);
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 2018-08-12
      • 1970-01-01
      相关资源
      最近更新 更多