Android 代码中修改控件宽高

不要在o nCreate()和onResume()方法中测量控件的宽高,不然得到的数据都是0,可以在onWindowFocusChanged()方法中测量
@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        int width = background.getWidth();
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        background.measure(w, h);
        ViewGroup.LayoutParams params = background.getLayoutParams();
        int height = (int) (width / 768.0 * 716);
        params.height = height;
        background.setLayoutParams(params);
    }

猜你喜欢

转载自blog.csdn.net/pxcz110112/article/details/80332986
今日推荐