安卓自定义布局如何插入到指定id的LinearLayout? 50
就像图一样,自定义一个组合控件,然后将activity_main.xml中的textview放到LinearLayout里面...
就像图一样,自定义一个组合控件,然后将activity_main.xml中的textview放到LinearLayout里面
展开
1个回答
展开全部
在MainActivity.java中添加下列代码
private LinearLayout myLayout;%定义对象
private LinearLayout.LayoutParams layoutP;
private int WC = LinearLayout.LayoutParams.WRAP_CONTENT;
private TextView black_TV, blue_TV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myLayout = new LinearLayout(this);
myLayout.setOrientation(LinearLayout.VERTICAL);
myLayout.setBackgroundResource(color.white);
setContentView(myLayout);
layoutP = new LinearLayout.LayoutParams(WC, WC);
black_TV = new TextView(this);
blue_TV = new TextView(this);
myLayout.addView(black_TV, layoutP);
myLayout.addView(blue_TV, layoutP);
black_TV.setTextColor(Color.BLACK);
blue_TV.setTextColor(Color.BLUE);
black_TV.setText("黑色");
blue_TV.setText("蓝色");
}
其中关于LayoutParams 不是很明白,网上一位朋友进行了详细叙述http://www.cnblogs.com/zhengbeibei/archive/2013/03/29/2989263.html
LayoutParams 实现动态添加控件,可以根据需求添加
private LinearLayout myLayout;%定义对象
private LinearLayout.LayoutParams layoutP;
private int WC = LinearLayout.LayoutParams.WRAP_CONTENT;
private TextView black_TV, blue_TV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myLayout = new LinearLayout(this);
myLayout.setOrientation(LinearLayout.VERTICAL);
myLayout.setBackgroundResource(color.white);
setContentView(myLayout);
layoutP = new LinearLayout.LayoutParams(WC, WC);
black_TV = new TextView(this);
blue_TV = new TextView(this);
myLayout.addView(black_TV, layoutP);
myLayout.addView(blue_TV, layoutP);
black_TV.setTextColor(Color.BLACK);
blue_TV.setTextColor(Color.BLUE);
black_TV.setText("黑色");
blue_TV.setText("蓝色");
}
其中关于LayoutParams 不是很明白,网上一位朋友进行了详细叙述http://www.cnblogs.com/zhengbeibei/archive/2013/03/29/2989263.html
LayoutParams 实现动态添加控件,可以根据需求添加
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询