2013-05-15 36 views
1

所以基本上我已經爲我創建的紙牌遊戲定義了一個片段,我希望將這個片段用於玩家1和玩家2(因爲它們都具有相同的佈局)但是我希望他們成爲彼此的鏡子。如何實現這一點,我有研究旋轉片段,但目前爲止沒有答案描述瞭如何在代碼中這樣做,他們只描述了屏幕旋轉時的處理。旋轉片段以在x軸上創建反射

了1v1的遊戲

package com.PigRam.magichelper; 

import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 

public class OneVsOneDuelActivity extends FragmentActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.one_vs_one_view); 
} 
} 

佈局的片段

<?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" 
android:baselineAligned="true"> 

<fragment android:name="com.PigRam.magichelper.PlayerOneDuelFragment" 
    android:id="@+id/player_one_fragment" 
    android:layout_weight="1" 
    android:layout_width="match_parent" 
    android:layout_height="0dp"/> 

<fragment android:name="com.PigRam.magichelper.PlayerTwoDuelFragment" 
    android:id="@+id/player_two_fragment" 
    android:layout_weight="1" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" /> 

</LinearLayout> 

活動目前的片段佈局只是在其佈局的一個按鈕,我想似乎表明一個玩家輪到了。

<?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:text="@string/end_turn" 
    android:id="@+id/end_turn_botton" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent"/> 

</LinearLayout> 

玩家1的代碼(玩家2是完全一樣的)

package com.PigRam.magichelper; 

import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 

public class OneVsOneDuelActivity extends FragmentActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.one_vs_one_view); 
} 
} 

回答

1

我猜,如果你使用的是Android 3.0 +,你可以重複使用相同的片段,並有2次實例旋轉它使用圖180度http://developer.android.com/reference/android/view/View.html#setRotation(float)(設置樞軸點之後爲0.5,0.5)

+0

所以我需要通過使用getById或類似的東西來獲得片段的每個實例,然後投射到片段然後旋轉? 或者我可以旋轉當前的視圖? –

+0

@Emil Adz我試過這個沒有任何運氣 –

+0

這是不可能的,我已經嘗試在我的Fragment類中調用container.setRotation,它會引發異常 –

0

添加片段的相對佈局和設置旋轉到180

<RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="150dp" 
      android:rotation="180"> 

      <fragment 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 

       android:layout_centerVertical="true" 
       android:layout_alignParentEnd="true" /> 
     </RelativeLayout> 
01是