Java 隐藏按钮时,重新排列布局中的按钮

Java 隐藏按钮时,重新排列布局中的按钮,java,android,android-layout,button,Java,Android,Android Layout,Button,我正在写一份申请书。它使用主布局 在它中,我使用相对布局,我有4个按钮 按钮1、按钮2、按钮3和按钮4 因此,基于用户凭据和功能,我隐藏了一些按钮。e、 g 管理员可以查看所有的按钮 按钮2和3对于用户帐户是隐藏的。所以当用户登录时,我只显示按钮1和按钮4 但是我想重新安排UI的外观 当前,当按钮2和3被隐藏时,当在屏幕上查看时,按钮1和4之间有一个很大的空间 当按钮2和3被隐藏时,我想将按钮1移动到按钮3的位置(靠近UI上的4)。这样在屏幕上看起来很好 我不知道如何做到这一点。当一个按钮被隐藏

我正在写一份申请书。它使用主布局

在它中,我使用相对布局,我有4个按钮

按钮1、按钮2、按钮3和按钮4

因此,基于用户凭据和功能,我隐藏了一些按钮。e、 g

管理员可以查看所有的按钮

按钮2和3对于用户帐户是隐藏的。所以当用户登录时,我只显示按钮1和按钮4

但是我想重新安排UI的外观

当前,当按钮2和3被隐藏时,当在屏幕上查看时,按钮1和4之间有一个很大的空间

当按钮2和3被隐藏时,我想将按钮1移动到按钮3的位置(靠近UI上的4)。这样在屏幕上看起来很好

我不知道如何做到这一点。当一个按钮被隐藏时,请让我知道如何重新排列布局中的按钮

 <RelativeLayout
        android:id="@+id/Info_RelativeLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="5dip"
        android:layout_marginTop="5dip" >
        <Button
            android:id="@+id/Camera_Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/division_LinearLayout"
            android:background="@drawable/camera_selector" />
        <Button
            android:id="@+id/Favorite_Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/GotoCamera_Button"
            android:background="@drawable/favorite_btn_selector" />
        <Button
            android:id="@+id/Profile_Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/Favorite_Button"
            android:background="@drawable/profile_list_btn_selector" />
        <Button
            android:id="@+id/Info_Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/ProfileList_Button"
            android:background="@drawable/camerainfo_selector" />
    </RelativeLayout>

尝试在按钮上使用setVisibility(View.GONE),而不仅仅是使它们不可见。当它们消失时,它们不会为布局目的占用空间,这与它们不可见时不同。

请尝试删除如下按钮:

   ViewGroup layout = (ViewGroup) btnToRemove.getParent();
   if(null!=layout) layout.removeView(btnToRemove); 

在活动中尝试
btn.setVisibility=View.GONE