在constraintlayout中获取子布局的高度和宽度

问题描述 投票:0回答:1

我有一个约束布局,其中有一个子布局(frame_layout)。使用match_constraint将宽度和高度设置为0dp。一旦在屏幕上加载布局,我们如何获得此frame_layout的高度和宽度?使用layoutparams给出0dp。 getWidth或getHeight再次给出0dp。

android android-constraintlayout
1个回答
1
投票
final FrameLayout layout = (FrameLayout) findViewById(R.id.YOUR_VIEW_ID);
ViewTreeObserver vto = layout.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener (new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() {
        layout.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
        int width  = layout.getMeasuredWidth();
        int height = layout.getMeasuredHeight(); 

    } 
});
© www.soinside.com 2019 - 2024. All rights reserved.