2012-02-08 108 views
0

我正在編碼一個Android遊戲,其中2個玩家同時玩。一名球員以正常方式面對電話,另一名球員則面朝下顛倒。Android顛倒佈局或旋轉180°

由於這是一個測驗,我沒有使用任何畫布或圖形。它只包含2個線性佈局,其中一個佈局應該是顛倒的。爲此,我使用了:

android:rotation="180" 

其中一個佈局。

它在Eclipse中我的xml的圖形視圖上顯示顛倒,但是當我在仿真器或手機上運行它時,它不會反轉或旋轉到180度。

XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" android:clipToPadding="false" 
     android:rotation="180"> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

    </LinearLayout> 

</LinearLayout 

Java代碼的

public class x extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 
} 

我如何可以旋轉它,無併發症。我目前正在使用API​​級別15,但我對API級別沒有限制。

回答

1

我會建議你創建一個自定義佈局時,你寫你的XML將處理旋轉它的onDraw()

public class RotatedLinearLayout extends RotatedLinearLayout { 
    final boolean topDown; 

    public RotatedLinearLayout (Context context){ 
    super(context); 
    } 

    public RotatedLinearLayout (Context context, AttributeSet attrs){ 
     super(context, attrs); 
    } 

    public RotatedLinearLayout (Context context, AttributeSet attrs, int defStyle){ 
     super(context, attrs,defStyle); 
    } 


    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ 
     super.onMeasure(heightMeasureSpec, widthMeasureSpec); 
     setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth()); 
    } 

    @Override 
    protected void onDraw(Canvas canvas){ 
     TextPaint textPaint = getPaint(); 
     textPaint.setColor(getCurrentTextColor()); 
     textPaint.drawableState = getDrawableState(); 

     canvas.save(); 

     if(topDown){ 
     canvas.translate(getWidth(), 0); 
     canvas.rotate(180); 
     }else { 
     canvas.translate(0, getHeight()); 
     canvas.rotate(-180); 
     } 
     canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop()); 
     getLayout().draw(canvas); 
     canvas.restore(); 
    } 
} 

,使用VerticalRelativeLayout而不是你嘗試創建其他佈局。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

    <path.to.package.RotatedLinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" android:clipToPadding="false" 
     > 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" /> 

    </path.to.package.RotatedLinearLayout > 

</LinearLayout 
+0

我使用任何相對佈局。 – akhil 2012-02-09 01:59:03

+0

我使用了線性佈局。 – akhil 2012-02-09 01:59:40

+0

是不是有一種方法,我可以使線性佈局旋轉,無需compilcating的東西。 – akhil 2012-02-09 02:00:26