Android:调整视图大小而不丢失位置
我有这个布局Android:调整视图大小而不丢失位置,android,android-layout,resize,layoutparams,Android,Android Layout,Resize,Layoutparams,我有这个布局 <RelativeLayout android:id="@+id/gameportView" android:layout_width="match_parent" android:layout_height="match_parent" > <com.pepotegames.spotthedifferences.DifferenceView android:id="@+id
<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>
一切都很好,只是我把一套新的布局参数传递给了我的酒吧。那么,如何调整进度条的大小而不丢失android:layout\u centerHorizontal=“true”和android:layout\u centerVertical=“true”属性?与其创建新的LayoutParams,不如尝试使用提取视图的当前LayoutParams,然后编辑所需的值。使用以下方法-
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);
}
});
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);
}
});