【问题标题】:Change button alignment of buttonA when buttonB is GONE按钮消失时更改按钮的按钮对齐方式
【发布时间】:2016-11-10 14:45:48
【问题描述】:

我在RelativeLayout 中放置了两个按钮prev 和next。当我到达应用程序的最后一页时,我正在禁用 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);
    }
}

【问题讨论】:

  • 发布您的布局代码...

标签: android button alignment android-relativelayout visibility


【解决方案1】:

当您使用View.Gone 时,它的删除视图位置和其他视图受到干扰

与

当您使用View.Invisible 时,它总是存在视图被隐藏并且其他视图不受干扰。

使用这个............

next.setVisibility(View.Invisible);

享受编码.....

【讨论】:

  • 但是,如果我将其设置为 INVISIBLE,prev 按钮将保留在原位。我不想要那个。它应该出现在中心。
【解决方案2】:

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

编辑

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

【讨论】:

    【解决方案3】:

    使用线性布局覆盖YOur主要相对布局内的按钮和设置

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

    到您的 prev 按钮。

    【讨论】:

    • 我有,但它没有公开显示,因为我的代表低于 15 岁
    猜你喜欢
    • 1970-01-01
    • 2019-04-01
    • 2013-10-21
    • 2016-05-29
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    相关资源
    最近更新 更多