Android 更改文本更改时编辑文本的宽度

Android 更改文本更改时编辑文本的宽度,android,android-edittext,Android,Android Edittext,我正在尝试更改搜索栏的宽度,这样当用户开始键入时,搜索栏会覆盖fav按钮,但当他们不键入fav按钮时,会显示fav按钮 这是我现在拥有的 searchBar.addTextChangedListener(new TextWatcher(){ public void afterTextChanged(Editable arg0){ menuItems.this.adapter.getFilter().filter(arg0);

我正在尝试更改搜索栏的宽度,这样当用户开始键入时,搜索栏会覆盖fav按钮,但当他们不键入fav按钮时,会显示fav按钮

这是我现在拥有的

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) {               
        }
    }); 
searchBar.addTextChangedListener(新的TextWatcher(){
public void PostTextChanged(可编辑arg0){
menuItems.this.adapter.getFilter().filter(arg0);

if(searchBar.getText().length()如果您的目标是隐藏/显示视图,我建议使用不同的方法(在您的例子中是Fav按钮)

试试这个:

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

if(searchBar.getText().length()您需要在AndroidManifest.xml中设置layout_width元素,如下所示

android:layout_width="fill_parent"
<EditText android:id="@+id/txtURL" android:layout_width="fill_parent"
...
...>
</EditText>
只需在EditText中设置此元素,如下所示

android:layout_width="fill_parent"
<EditText android:id="@+id/txtURL" android:layout_width="fill_parent"
...
...>
</EditText>

favButton确实会消失,但editText的宽度不会改变。请尝试使用Lucifer posted的代码。如果这不起作用,请编辑问题并添加声明按钮的方式,以便我们查看。