View如何方便的setWidth/Height及getWidt

2019-06-27  本文已影响0人  kongzue

先新建一个类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(宽度值);

//其他操作类似

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

上一篇下一篇

猜你喜欢

热点阅读