android studio 不用xml直接使用java代码进行布局.好的可以加分
实现如图所示,不用xml布局,直接java代码里写提示是用LayoutParams麻烦写下代码,可以加到满分。...
实现如图所示,不用xml布局,直接java代码里写提示是用LayoutParams麻烦写下代码,可以加到满分。
展开
2个回答
展开全部
// 使用代码布局
// root layout
LinearLayout root = new LinearLayout(this);
root.setOrientation(LinearLayout.VERTICAL);
// top layout
LinearLayout topLayout = new LinearLayout(this);
LinearLayout.LayoutParams topParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0);
topParams.weight = 1;
topLayout.setLayoutParams(topParams);
// left child
View leftView = new View(this);
leftView.setBackgroundColor(Color.RED);
LinearLayout.LayoutParams leftParams = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT);
leftParams.weight = 1;
leftView.setLayoutParams(leftParams);
topLayout.addView(leftView);
// right Layout
LinearLayout rightLayout = new LinearLayout(this);
rightLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams rightParams = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT);
rightParams.weight = 1;
rightLayout.setLayoutParams(rightParams);
// right top child
View rightTopView = new View(this);
rightTopView.setBackgroundColor(Color.YELLOW);
LinearLayout.LayoutParams rightTopParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0);
rightTopParams.weight = 1;
rightTopView.setLayoutParams(rightTopParams);
// add right top view
rightLayout.addView(rightTopView);
// right bottom child
View rightBottomView = new View(this);
rightBottomView.setBackgroundColor(Color.BLACK);
LinearLayout.LayoutParams rightBottomParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0);
rightBottomParams.weight = 1;
rightBottomView.setLayoutParams(rightBottomParams);
// add right bottom view
rightLayout.addView(rightBottomView);
// add right layout
topLayout.addView(rightLayout);
// add top layout
root.addView(topLayout);
// bottom layout
LinearLayout bottomLayout = new LinearLayout(this);
bottomLayout.setBackgroundColor(Color.BLUE);
LinearLayout.LayoutParams bottomParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0);
bottomParams.weight = 1;
bottomLayout.setLayoutParams(bottomParams);
root.addView(bottomLayout);
// setContentView(R.layout.activity_main);
setContentView(root);
2016-06-04
展开全部
调试麻烦,,,,,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询