我试图只是测试一下,并在蓝色视图中出现红色视图,但这不起作用?
import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class Game extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initialize();
}
protected void initialize() {
LinearLayout maze = new LinearLayout(this);
maze.setBackgroundColor(Color.BLUE);
maze.setLayoutParams(new LinearLayout.LayoutParams(40, 40));
LinearLayout maze1 = new LinearLayout(this);
maze1.setLayoutParams(new LinearLayout.LayoutParams(20, 20));
maze1.setBackgroundColor(Color.RED);
maze.addView(maze1);
setContentView(maze);
}
}
我更新了我的代码以包含整个班级。
答案 0 :(得分:0)
您还应该为根布局添加尺寸。
LinearLayout maze = new LinearLayout(this);
maze.setBackgroundColor(Color.BLUE);
LinearLayout maze1 = new LinearLayout();
maze1.setLayoutParams(new LinearLayout.LayoutParams(20, 20));
maze1.setBackgroundColor(Color.RED);
maze.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
maze.addView(maze1);
setContentView(maze);