如何在android searchview OnClose监听器中获取视图

时间:2018-05-29 14:10:13

标签: android

searchView = findViewById(R.id.searchicon);
    searchView.setOnSearchClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            v.getLayoutParams().width = 1800;
        }
    });
   searchView.setOnCloseListener(new SearchView.OnCloseListener() {
       @Override
       public boolean onClose() {

           return false;
       }
   });

我想减少searchviewOnCloseListener的宽度,如何获得该视图?

1 个答案:

答案 0 :(得分:1)

绘制完布局以改变尺寸和形状后,您必须调用requestLayout()

所以使用

searchView.setOnSearchClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            v.getLayoutParams().width = 1800;
            v.invalidate(); // for redrawing 
            v.requestLayout(); // for dimension change 
        }
    });

封闭使用

searchView.setOnCloseListener(new SearchView.OnCloseListener() {
       @Override
       public boolean onClose() {
           searchView.requestLayout(); // make sure to declare search view outside oncreate or use local final variable
           // or this //findViewById(R.id.searchicon).requestLayout(); // for dimension change 
           return false;
       }
   });

Reference: Usage of forceLayout(), requestLayout() and invalidate()