2013-01-25 16 views
0

我有以下代碼,其中MyClass基本上擴展了View。我想知道是否需要同時使用setContentView(R.layout.activity_mainlcass_app)setContentView(myDrawing)來顯示我在MyClass中繪製的2D圖形。在一個活動類中使用兩個setContentView

public class MainClass extends Activity { 
    MyClass myDrawing; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_mainlcass_app); 

     myDrawing = new myDrawing(this); 
      setContentView(myDrawing); 
      myDrawing.requestFocus(); 
    } 
} 
+1

你爲什麼要這樣實現?您可以在同一活動中設置多個視圖,但您需要根據某些條件對其進行充氣。您無法在同一時間膨脹兩個視圖。 – GrIsHu

+0

你可以使用addContentView爲你的自定義視圖來繪製在頂部ov從xml膨脹的視圖..你試過.. ..? –

回答

2

不,你不能那樣做。第二個佈局將在父視圖上覆蓋。

+0

非常感謝。我不確定我能否做到這一點。 – MKS

2

在您主要佈局(activity_mainlcass_app)只需添加MyClass

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

    <com.example.MyClass 
     android:id="@+id/myclass" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</RelativeLayout> 
0

嘗試這樣

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

<com.example.firstactivity 
android:layout_width="fill_parent" 
    android:layout_height="fill_parent"/> 


<com.example.secondactivity 
android:layout_width="fill_parent" 
    android:layout_height="fill_parent"/> 

</LinearLayout> 

對於如果您希望有新的更多信息,請查看本linkexample

0

繪製其他(不同的ent佈局大小可能)使用FrameLayout進行原始佈局,並使用重力來定位新繪圖並使用addView方法進行活動。

相關問題