在你的XML佈局,給你的SurfaceView一個名字加入name屬性:
<com.nelsondev.myha3ogl.M3View
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/my_surfaceView1"/>
接下來,覆蓋的onPause和的onResume在活動中,通過使用findViewById(R.id.my_surfaceView1);
找到視圖,然後調用的onPause和的onResume您surfaceView:
@Override
public void onPause(){
com.nelsondev.myha3ogl.M3View myView = (com.nelsondev.myha3ogl.M3View)findViewById(R.id.my_surfaceView1);
myView.onPause();
super.onPause();
}
最後,在你實現你的面來看,覆蓋的onPause()/的onResume(),把你需要做的任何代碼,當你的活動暫停/在那裏重新開始。也請記住調用super.onPause()/ super.onResume()在表面觀
編輯:我只想澄清,你可以使用任何的ViewGroup對象
findViewById()
方法來發現裏面的ViewGroup子視圖:
MyActivity extends Activity{
public void onCreate(Bundle bundle){
FrameLayout myFrameLayout = (FrameLayout)getLayoutInflater().inflate(R.layout.graphics, null, false);
TextView myView = (TextView)myFrameLayout.findViewById(R.id.textView1);
if(myView!=null){
myView.setText("about to be removed");
myFrameLayout.removeView(myView);
}
setContentView(myFrameLayout);
}
}
或者findViewById()
也是活動的方法,它會發現在任何佈局視圖中,您設置使用setContentView();
MyActivity extends Activity{
public void onCreate(Bundle bundle){
setContentView(R.layout.graphics);
// where the xml file in your question is called graphics.xml
com.nelsondev.myha3ogl.M3View myGLSurfaceView = (com.nelsondev.myha3ogl.M3View)findViewById(R.id.my_surfaceView1);
FrameLayout myFrameLayout = (FrameLayout)findViewById(R.id.framelay);
}
}
謝謝!我多次使用findViewById來獲取按鈕和編輯控件等,但是我沒有意識到我可以像這樣使用自定義類。它完美運作;再次感謝! –