2016-03-02 57 views
1

我通过编程创建了几个布局,并且我需要获取其中一个宽度,重新计算此值并进行更改。我在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 

       } 
      }); 
     } 

回答

2

尝试添加该到GlobalLayoutListener结束:

rootLayout.requestLayout(); 

这应该迫使布局重绘它的大小。如果这不起作用,您可能还想尝试创建并设置新的LayoutParams。

+0

伙计,它的魔力) –

+0

不错!你拯救我的一天! – kristyna