2013-06-26 25 views
0

我有一個按鈕,它在-90旋轉。旋轉的按鈕不與左對齊。旋轉垂直按鈕不與父對齊

如下因素是XML

<RelativeLayout 
     android:id="@+id/rl_showChapters" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:background="#000000" 
     android:visibility="gone" > 

     <Button 
      android:id="@+id/btn_show" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true" 
      android:rotation="-90" 
      android:text=" Show Chapters " /> 

    </RelativeLayout> 

以下爲輸出。 enter image description here

但我想堅持這個按鈕左paren。

回答

0

你在說android:layout_centerVertical="true"ButtonRelativeLayout。修改它,因爲

android:layout_centerHorizontal="true" 

,則該按鈕將對齊的RelativeLayout

+0

還是一樣。不用找了。 –

+0

你是否刪除過'android:layout_centerVertical =「true」'或更改了'android:layout_centerVertical =「false」' –

+0

是的我已經刪除了'android:layout_centerVertical =「true」' –

1

左我知道這是一個老問題,但我想在這裏放下一個答案以備將來參考。

我和我想要將按鈕對齊到其父項右側的異常完全相同。這裏的問題是,旋轉視圖時,選擇樞軸爲中心,除非另有說明(我不知道該怎麼做)。我找不到任何屬性來爲我完成這項工作;所以我實現了以下方法:

private void transformShowPopupButton() { 
    int w = btn_showPopup.getWidth(), h = btn_showPopup.getHeight(); 
    int diff = w/2-h/2; 
    btn_showPopup.setRotation(270); 
    btn_showPopup.setTranslationX(diff); 
} 

基本上,此方法移動按鈕,直到它與其父對齊。移動量等於getWidth()/2getHeight()/2之間的差值(當然,我沒有任何保證金,並且我的按鈕與其父母對齊,如果您指定了保證金值,則還需要添加該保證金值。)

要將按鈕對齊到其父項的左側,您需要按照-diff來翻譯它。

重要提示:爲了能夠得到一個視圖的高度和寬度,btn_showPopup在我的情況下,調用transformShowPopupButton()onCreate()將無法​​正常工作。所以在onWindowFocusChanged()之內調用它就行。

@Override 
public void onWindowFocusChanged(boolean hasFocus) { 
    super.onWindowFocusChanged(hasFocus); 
    transformShowPopupButton(); 
}