这是一个玩具的例子。如果您编译并在实际设备(而不是模拟器)上进行测试,您将看到第一次连接SurfaceView时,整个屏幕内容会相应更改。然而,在按下然后再次按下按钮时,您将看到顶部的一条小线仍然是前一屏幕的蓝色,并且仅在创建Surface后消失。这在我的应用程序中发生在我的所有视图中(没有SurfaceView)并且分散了烦恼的注意力。有没有人见过这个问题?有解决方法吗?我在Android 2.1上的HTC EVO上运行它
package com.example.surfaceviewtest;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
public class MainActivity extends Activity {
private RelativeLayout home;
private RelativeLayout newView;
private int view;
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
home = new RelativeLayout(this);
home.setBackgroundColor(Color.BLUE);
Button changeCV = new Button(this);
changeCV.setText("Change Content View");
changeCV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setContentView(newView, new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
view = 2;
}
});
RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
buttonParams.addRule(RelativeLayout.CENTER_IN_PARENT);
home.addView(changeCV, buttonParams);
newView = new RelativeLayout(this);
RelativeLayout.LayoutParams surfaceParams = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, 200);
surfaceParams.addRule(RelativeLayout.CENTER_IN_PARENT);
SurfaceView surfView = new SurfaceView(this);
newView.addView(surfView, surfaceParams);
RelativeLayout.LayoutParams tvParams = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tvParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
TextView tv = new TextView(this);
newView.addView(tv, tvParams);
setContentView(home, new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
view = 1;
}
/* (non-Javadoc)
* @see android.app.Activity#onBackPressed()
*/
@Override
public void onBackPressed() {
if (view == 2) {
setContentView(home, new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
view = 1;
}
else {
finish();
}
}
}