获取布局宽度并在 onGlobalLayoutListener 中设置新的宽度值

Get layout width and set new width value in onGlobalLayoutListener

我以编程方式创建了几个布局,我需要获取其中一个的宽度,重新计算该值并进行更改。我在 onGlobalLayoutListener 中执行的所有这些操作。但是当我设置一个新的宽度值时,他并没有改变。

private RelativeLayout batteryContainer;
    private LinearLayout batteryLeftSide;
    private LinearLayout batteryRightSide;

    public void drawBattery(){
            handler.post(new Runnable() {
                @Override
                public void run() {

                    //Контейнер всієї батарейки
                    batteryContainer = new RelativeLayout(activity);
                    RelativeLayout.LayoutParams batteryContainerParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                    batteryContainerParams.setMargins((int) convertDpToPixel(containerMarginLeft), (int) convertDpToPixel(containerMarginTop), (int) convertDpToPixel(containerMarginRight), (int) convertDpToPixel(containerMarginBottom));
                    if(layoutBelow != null){
                        batteryContainerParams.addRule(RelativeLayout.BELOW, layoutBelow.getId());
                    }
                    if(layoutAbove != null){
                        batteryContainerParams.addRule(RelativeLayout.ABOVE, layoutAbove.getId());
                    }
                    batteryContainer.setLayoutParams(batteryContainerParams);
                    batteryContainer.setBackgroundColor(Color.parseColor("#505050"));
                    batteryContainer.setId(containerId);

                    int leftWidth = 0;
                    int rightWidth = 0;

                    //Ліва частина батарейки
                    batteryLeftSide = new LinearLayout(activity);
                    batteryLeftSide.setOrientation(LinearLayout.HORIZONTAL);
                    RelativeLayout.LayoutParams batteryLeftSideParams = new RelativeLayout.LayoutParams((int)convertDpToPixel(leftWidth), (int)convertDpToPixel(sideHeight));
                    batteryLeftSideParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    batteryLeftSide.setLayoutParams(batteryLeftSideParams);
                    batteryLeftSide.setBackgroundColor(Color.parseColor("#900000"));
                    batteryLeftSide.setId(leftSideId);

                    //Права частина батарейки
                    batteryRightSide = new LinearLayout(activity);
                    batteryRightSide.setOrientation(LinearLayout.HORIZONTAL);
                    RelativeLayout.LayoutParams batteryRightSideParams = new RelativeLayout.LayoutParams((int)convertDpToPixel(rightWidth), (int)convertDpToPixel(sideHeight));
                    batteryRightSideParams.addRule(RelativeLayout.RIGHT_OF, batteryLeftSide.getId());
                    batteryRightSide.setLayoutParams(batteryRightSideParams);
                    batteryRightSide.setBackgroundColor(Color.parseColor("#009900"));
                    batteryRightSide.setId(rightSideId);

                    //Додамо праві та ліві сторони в контейнер
                    batteryContainer.addView(batteryLeftSide);
                    batteryContainer.addView(batteryRightSide);
                    //Додамо контейнер в кореневий Layout на активності
                    rootLayout.addView(batteryContainer);

                    //Обчислення яка сторона батарейки займе 50%
                    ViewTreeObserver observer = rootLayout.getViewTreeObserver();
                    observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                        @Override
                        public void onGlobalLayout() {
                            rootLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                            int batteryContainerWidth = batteryContainer.getMeasuredWidth();
                            int halfPlace = batteryContainerWidth * 50 / 100;
                            trustFromMe = halfPlace;
                            if(trustToMe > trustFromMe){
                                batteryLeftSide.getLayoutParams().width = halfPlace;
                            }
                            if(trustToMe < trustFromMe){
                                batteryRightSide.getLayoutParams().width = halfPlace;
                            }
                            if(trustToMe == trustFromMe){
                                batteryLeftSide.getLayoutParams().width = halfPlace;
                                batteryRightSide.getLayoutParams().width = halfPlace;
                            }
                        }
                    });
                    //but width not change

                }
            });
        }

尝试将此添加到 GlobalLayoutListener 的末尾:

rootLayout.requestLayout();

这应该会强制布局重绘其大小。如果这不起作用,您可能还想尝试创建和设置新的 LayoutParams。