2016-03-02 33 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