我正在创建在屏幕上绘制正方形的基本应用程序。每次启动我的应用程序后都会立即消失。这是我的代码:
我在 MainActivity 中删除了自动创建的方法,例如 onCreateOptionsMenuSelected
public class MainActivity extends AppCompatActivity {
public static int WIDTH;
{
WIDTH = getWindowManager().getDefaultDisplay().getWidth();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View customView=new CustomView(this);
customView.setBackgroundColor(Color.BLACK);
setContentView(customView);
}
}
下一节课
public class CustomView extends View {
public static final int ELEMENTS=15;
private Paint paint=new Paint();
int x = 50;
int y = 50;
/////////////////////////////////////////////////////////
public CustomView(Context context){
super(context);
}
////////////////////////////////////////////////////////////////////
@Override
public void onDraw(Canvas canvas){
paint.setColor(Color.BLACK);
canvas.drawRect(30, 30, 80, 80, paint); //some arbitrary numbers
}
}