修改布局参数时,按钮边距错误

时间:2020-07-28 18:48:03

标签: java xml android-studio android-layout

我有一个 Relative Layout 活动,其中有一个圆形进度条和一个Button,进度条顶部的Button边距设置为159,Button位于进度条的中间。 / p>

如果我运行该应用程序,该按钮将停留在进度条的中间,但是,当我运行该应用程序时,我需要更改边距值,因此,我将其设置为159,而它没有停留在中间不再。

这是未修改保证金值时活动的图像:

enter image description here

这是当边距值通过编程方式设置为159时活动的图像:

enter image description here

如您所见,绿色按钮不在进度栏中间。

我用于更改边距值的代码:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) myButton.getLayoutParams();
params.setMargins(0, 159, 0, 0);
myButton.setLayoutParams(params);

这是按钮进度栏 xml代码:

<ProgressBar
    android:id="@+id/progressBar"
    style="@style/Widget.AppCompat.ProgressBar.Horizontal"
    android:layout_width="340dp"
    android:layout_height="340dp"
    android:layout_centerInParent="true"
    android:progress="100"
    android:progressDrawable="@drawable/degrees_circle" />

<Button
    android:id="@+id/myButton"
    android:layout_width="22dp"
    android:layout_height="22dp"
    android:layout_alignTop="@+id/progressDegrees"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="159dp"
    android:background="@drawable/circle_shape"
    android:backgroundTint="@color/colorPrimary"
    android:fontFamily="sans-serif-medium"
    android:text="x"
    android:textColor="#fff"
    android:textSize="15dp" />

问题是,是什么原因导致此问题?

0 个答案:

没有答案