activity中获取view宽高的几种方式

public class MainActivity extends BaseActivity{
    private View v_view1;
    private View v_view2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        v_view1 = findViewById(R.id.v_view1);
        v_view2 = findViewById(R.id.v_view2);

        L.i("normal: v_view1.getWidth():" + v_view1.getWidth()
                + "  v_view1.getHeight():" + v_view1.getHeight());
//-------------------------------------------------------------------
        v_view1.post(new Runnable() {
            @Override
            public void run() {
                L.i("post(Runnable) : v_view1.getWidth():" + v_view1.getWidth()
                        + "  v_view1.getHeight():" + v_view1.getHeight());
            }
        });
//------------------------------------------------------------------------
        v_view1.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                L.i("ViewTreeObserver : v_view1.getWidth():" + v_view1.getWidth()
                        + "  v_view1.getHeight():" + v_view1.getHeight());
            }
        });
//-----------------------------------------------------------------------------
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        L.i("onWindowFocusChanged : v_view1.getWidth():" + v_view1.getWidth()
                + "  v_view1.getHeight():" + v_view1.getHeight());
    }
}
//---------------------下面这个不靠谱--------------------------------------------------------
        int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec((1<<30)-1, View.MeasureSpec.AT_MOST);
        int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec((1<<30)-1, View.MeasureSpec.AT_MOST);
        v_view1.measure(widthMeasureSpec, heightMeasureSpec);
        L.i("measure : v_view1.getMeasuredWidth():" + v_view1.getMeasuredWidth()
                + "  v_view1.getMeasuredHeight():" + v_view1.getMeasuredHeight());
        v_view2.measure(widthMeasureSpec, heightMeasureSpec);
        L.i("measure : v_view2.getMeasuredWidth():" + v_view2.getMeasuredWidth()
                + "  v_view2.getMeasuredHeight():" + v_view2.getMeasuredHeight());

    }

log日志

I/[PID:2659]: [TID:1] MainActivity.onCreate(line:28): normal: v_view1.getWidth():0 v_view1.getHeight():0
I/[PID:2659]: [TID:1] MainActivity.onCreate(line:50): measure : v_view1.getMeasuredWidth():144 v_view1.getMeasuredHeight():144
I/[PID:2659]: [TID:1] MainActivity.onCreate(line:53): measure : v_view2.getMeasuredWidth():16777215 v_view2.getMeasuredHeight():16777215
I/[PID:2659]: [TID:1] 2.onGlobalLayout(line:42): ViewTreeObserver : v_view1.getWidth():144 v_view1.getHeight():144
I/[PID:2659]: [TID:1] 1.run(line:34): post(Runnable) : v_view1.getWidth():144 v_view1.getHeight():144
I/[PID:2659]: [TID:1] MainActivity.onWindowFocusChanged(line:61): onWindowFocusChanged : v_view1.getWidth():144 v_view1.getHeight():144

你可能感兴趣的:(activity中获取view宽高的几种方式)