屏幕是白色的,并且 onDraw 中的 System.out.println() 从未被调用过。
这是我的 activity_main.xml
<foo.packagename.MyView
android:id="@+id/myView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
自定义视图类:
public class MyView extends View {
public MyView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
System.out.println("This line is called");
while (true) {
invalidate();
}
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.GREEN);
System.out.println("This line is never printed");
}
主要活动:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
System.out.println("This line is called");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View myView = findViewById(R.id.myView);
}
}