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 定位的。问题是我需要在运行时调整 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_centerHorizo​​ntal="true" 和 android:layout_centerVertical="true" 属性的情况下调整进度条的大小?

4

2 回答 2

0

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

于 2013-09-26T17:25:12.647 回答
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);
    }
});
于 2013-09-26T17:31:28.243 回答