【问题标题】:Android: Resize View without losing positionAndroid:在不丢失位置的情况下调整视图大小
【发布时间】:2013-09-26 17:18:57
【问题描述】:

我有这个布局

<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" 属性的情况下调整进度条的大小?

【问题讨论】:

    标签: android android-layout resize layoutparams


    【解决方案1】:

    使用以下 -

    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);
        }
    });
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2013-05-04
        • 1970-01-01
        • 2016-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-02
        • 2018-05-20
        • 1970-01-01
        相关资源
        最近更新 更多