2013-09-26 57 views
0

我有这样的布局安卓:调整视图不失位置

<RelativeLayout 
     android:id="@+id/gameportView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <com.pepotegames.spotthedifferences.DifferenceView 
      android:id="@+id/imageA" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_above="@+id/progressBar1" 
      android:layout_centerHorizontal="true" /> 

     <ProgressBar 
      android:id="@+id/progressBar1" 
      style="?android:attr/progressBarStyleHorizontal" 
      android:layout_width="match_parent" 
      android:layout_height="16dp" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" 
      android:max="1000" 
      android:progress="0" /> 

     <com.pepotegames.spotthedifferences.DifferenceView 
      android:id="@+id/imageB" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@+id/progressBar1" 
      android:layout_centerHorizontal="true" /> 

    </RelativeLayout> 

正如你所看到的,imageA和imageB相对于定位到我的进度。问题是我需要在运行时调整ProgressBar的大小。为此,我有一个自定义侦听器,当我的自定义View onSizeChanged方法被调用时触发。

imageA.setOnSizeChangedLister(new DifferenceView.OnSizeChangedListener(){ 
     @Override 
     public void onResize(){ 
      level.scaleBy(imageA.getScale()); 

      bar.setLayoutParams(new RelativeLayout.LayoutParams(imageA.getWidth(),bar.getHeight())); 
     } 
    }); 

一切工作正常,除了im传递一组新的LayoutParams到我的酒吧。那么,如何调整进度条的大小而不会丢失android:layout_centerHorizo​​ntal =“true”和android:layout_centerVertical =“true”属性?

回答

0

使用以下 -

imageA.setOnSizeChangedLister(new DifferenceView.OnSizeChangedListener(){ 
    @Override 
    public void onResize(){ 
     level.scaleBy(imageA.getScale()); 

     RelativeLayout.LayoutParams existingLayoutParams = ((RelativeLayout.LayoutParams) bar.getLayoutParams()); 
     existingLayoutParams.width = imageA.getWidth(); 
     existingLayoutParams.height = imageA.getHeight(); 
     bar.setLayoutParams(existingLayoutParams); 
    } 
}); 
0

而不是创建新的LayoutParams的,请尝试使用getLayoutParams提取查看当前的LayoutParams,然后编辑所需值。