2016-07-08 33 views
2

我在RelativeLayout中放置了兩個按鈕prevnext。當我到我的應用程序的最後一頁,我禁止使用next按鈕:當buttonB爲GONE時更改buttonA的按鈕對齊方式

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

發佈您的佈局代碼... –

回答

2

這裏不要使用相對佈局,使用線性佈局相反。 由於在相對佈局中,視圖相對於彼此進行定位,因此當您刪除一個視圖時,它可能會中斷針對已刪除視圖定位的其他視圖。

編輯

使用可以使用您的看法如下屬性:weightSum,重量和填充。

+0

明白了,謝謝.. –

1

當您使用View.Gone其移除View到位等觀點干擾

在使用View.Invisible它總是在那裏只是視圖是隱藏別人對你的看法不打擾

使用本...........

next.setVisibility(View.Invisible); 

享受編碼.....

+0

但是,如果我將其設置爲INVISIBLE,則「prev」按鈕將保留在它的位置。我不想那樣。它應該出現在中心。 –

2

使用線性佈局既包括內,您的主要相對佈局的按鈕和並設置

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

上一頁按鈕。

+0

它的工作,謝謝。 –

+0

如果您的工作正常,請+1我的答案。 – Saini

+0

我有,但沒有公開顯示,因爲我的代表低於15 –