美文网首页
View如何方便的setWidth/Height及getWidt

View如何方便的setWidth/Height及getWidt

作者: kongzue | 来源:发表于2019-06-27 19:48 被阅读0次

    先新建一个类ViewWrapper:

    public class ViewWrapper {
        
        private View mTargetView;
        
        public ViewWrapper(View target) {
            mTargetView = target;
        }
        
        public int getWidth() {
            return mTargetView.getLayoutParams().width;
        }
        
        public int getHeight() {
            return mTargetView.getLayoutParams().height;
        }
        
        public void setWidth(int width) {
            mTargetView.getLayoutParams().width = width;
            mTargetView.requestLayout();
        }
        
        public void setWidth(float width) {
            mTargetView.getLayoutParams().width = (int) width;
            mTargetView.requestLayout();
        }
        
        public void setHeight(int height) {
            mTargetView.getLayoutParams().height = height;
            mTargetView.requestLayout();
        }
        
        public void setHeight(float height) {
            mTargetView.getLayoutParams().height = (int) height;
            mTargetView.requestLayout();
        }
    }
    

    然后使用:

    ViewWrapper vw = new ViewWrapper(你的View);
    vw.setWidth(宽度值);
    
    //其他操作类似
    

    来源网络,原作者未知,侵权请告知备注原作者姓名或删除。

    相关文章

      网友评论

          本文标题:View如何方便的setWidth/Height及getWidt

          本文链接:https://www.haomeiwen.com/subject/wgrdcctx.html