0

我正在尝试更改我的搜索栏的宽度,以便当用户开始输入搜索栏时会覆盖收藏按钮,但当他们不输入收藏按钮时会显示。

这是我现在拥有的

searchBar.addTextChangedListener(new TextWatcher(){         
        public void afterTextChanged(Editable arg0){
            menuItems.this.adapter.getFilter().filter(arg0);
            if(searchBar.getText().length() <= 0){

                searchBar.getLayoutParams().width = 280;
                favButton.getLayoutParams().width = 15;
            }
            else{                   
                searchBar.getLayoutParams().width = 295;
                favButton.getLayoutParams().width = 0;
            }
        }
        public void beforeTextChanged(CharSequence s, int start, int count,int after) {
        }
        public void onTextChanged(CharSequence s, int start, int before, int count) {               
        }
    }); 
4

2 回答 2

1

如果您的目标是隐藏/显示视图(在您的情况下为收藏按钮),我建议使用不同的方法。

尝试这个:

if(searchBar.getText().length() <= 0) {
    searchBar.getLayoutParams().width = 280;
    favButton.setVisibility(View.Visible);
} else {                   
    searchBar.getLayoutParams().width = 295;
    favButton.setVisibility(View.GONE);
}

很难说在不了解您的布局的情况下它的工作效果如何,但使用固定尺寸大小通常不是一个很好的做法。

于 2012-09-26T16:07:29.637 回答
0

您需要在 AndroidManifest.xml 中设置 layout_width 元素,如下所示,

android:layout_width="fill_parent"

只需在您的 EditText 中设置此元素,如下所示,

<EditText android:id="@+id/txtURL" android:layout_width="fill_parent"
...
...>
</EditText>
于 2012-09-26T16:11:02.783 回答