按钮消失时更改按钮的按钮对齐方式

Change button alignment of buttonA when buttonB is GONE

我在 RelativeLayout 中放置了两个按钮 prevnext。当我到达应用程序的最后一页时,我使用 :

禁用 next 按钮
next.setVisibility(View.GONE);

prev 按钮的排列被打乱。我希望它与 RelativeLayout 的中心对齐,就好像只有一个按钮一样。

这是我的代码:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_below="@+id/radgroup"
    >
    <Button
        android:layout_marginTop="14dp"
        android:layout_marginLeft="20dp"
        android:text="@string/previous"
        android:textAllCaps="false"
        android:textSize="23dp"
        android:layout_width="150dp"
        android:layout_height="70dp"
        android:id="@+id/prev"
        android:onClick="viewPreviousQuestion"
        />
    <Button
        android:layout_marginTop="14dp"
        android:layout_marginLeft="20dp"
        android:text="@string/previous"
        android:textAllCaps="false"
        android:textSize="23dp"
        android:layout_width="150dp"
        android:layout_height="70dp"
        android:layout_toRightOf="@+id/prev"
        android:id="@+id/nxt"
        android:onClick="viewNextQuestion"
        />

</RelativeLayout>

并且 onClick 事件是

public void viewNextQuestion(View view) {
    if(currqstn==lastqstn){
        next.setVisibility(View.GONE);
    }
}

这里不要使用相对布局,而是使用线性布局。 由于在相对布局中视图是相对于彼此定位的,因此当您删除一个视图时,它可能会破坏相对于已删除视图定位的其他视图。

编辑

Use 可以为您的视图使用以下属性:weightSum、weight 和 padding。

当您使用 View.Gone 时,它删除了视图位置并且 其他视图被打扰了

AND

当您使用 View.Invisible 时,它总是在那里 视图被隐藏 并且 其他视图不会被打扰.

使用这个......

next.setVisibility(View.Invisible);

享受编码......

使用线性布局覆盖您主要相对布局中的两个按钮并设置

android:layout_weight="1"
android:layout_gravity="center_horizontal|center"

转到您的 上一个 按钮。