我有这个布局
<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定位。问题是我需要在运行时调整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()));
}
});
除了将一组新的LayoutParams传递到我的栏之外,一切正常。那么,如何在不丢失android:layout_centerHorizontal =“true”和android:layout_centerVertical =“true”属性的情况下调整进度条的大小?
答案 0 :(得分:0)
尝试使用getLayoutParams提取视图的当前LayoutParams,然后编辑所需的值,而不是创建新的LayoutParams。
答案 1 :(得分: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);
}
});