Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 获取布局宽度并在onGlobalLayoutListener中设置新的宽度值_Android_Layout_Width - Fatal编程技术网

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

Android 获取布局宽度并在onGlobalLayoutListener中设置新的宽度值,android,layout,width,Android,Layout,Width,我以编程方式创建了几个布局,我需要得到其中一个,重新计算这个值并进行更改。我在onGlobalLayoutListener做的所有这些动作。但是当我设置一个新的宽度值时,他并没有改变 private RelativeLayout batteryContainer; private LinearLayout batteryLeftSide; private LinearLayout batteryRightSide; public void drawBattery(){

我以编程方式创建了几个布局,我需要得到其中一个,重新计算这个值并进行更改。我在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

                }
            });
        }
private RelativeLayout电池容器;
私人线路布局电池区;
私人线路布置在右侧;
公营机构{
handler.post(新的Runnable(){
@凌驾
公开募捐{
//Контейнер всієї батарейки
电池容器=新的相对物(活动);
RelativeLayout.LayoutParams batteryContainerParams=新的RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_父项,RelativeLayout.LayoutParams.WRAP_内容);
batteryContainerParams.setMargins((int)convertDpToPixel(ContainerArginLeft),(int)convertDpToPixel(ContainerArginTop),(int)convertDpToPixel(ContainerArginRight),(int)convertDpToPixel(ContainerArginBottom));
如果(布局低于!=null){
batteryContainerParams.addRule(RelativeLayout.down、LayoutDown.getId());
}
如果(layoutAbove!=null){
batteryContainerParams.addRule(RelativeLayout.over,layoutAbove.getId());
}
batteryContainer.setLayoutParams(batteryContainerParams);
batteryContainer.setBackgroundColor(Color.parseColor(#505050”);
电池容器.setId(集装箱ID);
int leftWidth=0;
int rightWidth=0;
//Ліва частина батарейки
batteryLeftSide=新的线性布局(活动);
电池侧设置方向(线性布局。水平);
RelativeLayout.LayoutParams batteryLeftSideParams=新的RelativeLayout.LayoutParams((int)convertDpToPixel(leftWidth),(int)convertDpToPixel(sideHeight));
batteryLeftSideParams.addRule(RelativeLayout.ALIGN\u PARENT\u LEFT);
batteryLeftSide.setLayoutParams(batteryLeftSideParams);
batteryLeftSide.setBackgroundColor(Color.parseColor(#900000”);
batteryLeftSide.setId(leftSideId);
//Права частина батарейки
蓄电池右侧=新的线性布局(活动);
蓄电池右侧。设置方向(线性布局。水平);
RelativeLayout.LayoutParams batteryRightSideParams=新的RelativeLayout.LayoutParams((int)convertDpToPixel(rightWidth),(int)convertDpToPixel(sideHeight));
batteryRightSideParams.addRule(RelativeLayout.RIGHT_OF,batteryLeftSide.getId());
batteryRightSide.setLayoutParams(batteryRightSideParams);
batteryRightSide.setBackgroundColor(Color.parseColor(“#009900”));
电池右侧。setId(右侧ID);
//Додамо праві та ліві сторони в контейнер
batteryContainer.addView(batteryLeftSide);
batteryContainer.addView(batteryRightSide);
//平面布置图
rootLayout.addView(电池容器);
//Обчислення яка сторона батарейки займе 50%
ViewTreeObserver observer=rootLayout.getViewTreeObserver();
observer.addOnGlobalLayoutListener(新的ViewTreeObserver.OnGlobalLayoutListener(){
@凌驾
公共图书馆{
getViewTreeObserver().removeOnGlobalLayoutListener(此);
int batteryContainerWidth=batteryContainer.getMeasuredWidth();
int halfPlace=电池盒宽度*50/100;
信任我=中间位置;
如果(信任我>信任我){
batteryLeftSide.getLayoutParams().width=halfPlace;
}
如果(信任我<信任我){
batteryRightSide.getLayoutParams().width=halfPlace;
}
if(trustToMe==trustFromMe){
batteryLeftSide.getLayoutParams().width=halfPlace;
batteryRightSide.getLayoutParams().width=halfPlace;
}
}
});
//但宽度不变
}
});
}

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

rootLayout.requestLayout();

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

Nice!你救了我一天!