在普通activity下布置代码逻辑
1. 设置最基础的Layout对象实例作为根View
2. 对于组件child,先设置组件甲相对于其父view的LayoutParams属性params,然后再设置组件child本身的属性,最后父view调用addview(View child, LayoutParams params)即可将组件甲动态的加入父view中:
代码范例:
initWelPage = new RelativeLayout(_context); initWelPage.setTag(initWelPage); initWelPage.setBackgroundResource(R.drawable.desert); setContentView(initWelPage); RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,280); layout.addRule(RelativeLayout.ALIGN_PARENT_LEFT); layout.addRule(RelativeLayout.ALIGN_PARENT_TOP); Utils.lady_gentle = new TextView(_context); Utils.lady_gentle.setId(1); Utils.lady_gentle.setBackgroundDrawable(null); Utils.lady_gentle.setTextSize(30); Utils.lady_gentle.setGravity(Gravity.LEFT); Utils.lady_gentle.setTextColor(Color.YELLOW); Utils.lady_gentle.setText(R.string.hello); Utils.lady_gentle.setTypeface(Typeface.DEFAULT,Tpyeface.BOLD); initWelPage.addView(Utils.lady_gentle,layout);
另一段范例: 以RelativeLayout型对象language_bg作为根view,RelativeLayout型对象lang_body放置在父对象language_bg的中间位置,RelativeLayout型对象lang_part1位于父对象lang_body的顶部位置,ImageButton型对象language_cance放置于父对象lang_part1的右上方,最后把根view作为方法的返回值返回给调用方法的对象。
public View initPopWindow(Context _contex) { language_bg = new RelativeLayout(_context); language_bg.setTag(language_bg); language_bg.setBackgroundColor(0xb0000000); RelativeLayout.LayoutParams layout1 = new RelativeLayout.LayoutParams(800,600); layout1.addRule(RelativeLayout.CENTER_IN_PARENT); lang_body = new RelativeLayout(_context); lang_body.setTag(lang_body); lang_body.setBackgroundColor(0xe0000000); language_bg.addView(lang_body, layout1); RelativeLayout.LayoutParams layout2 = new RelativeLayout.LayoutParams(ViewGroup.Layoutparams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONENT); layout2.addRule(RelativeLayout.ALIGN_PARENT_TOP); lang_part1 = new RelativeLayout(_context); lang_part1.setTag(lang_part1); lang_part1.setId(1); lang_part1.setBackgroundColor(0xe0000000); lang_body.addView(lang_part1, layout2); RelativeLayout.LayoutParams layout3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); layout3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); language_cancel = new ImageButton(_context); language_cancel.setTag(language_cancel); language_cancel.setBackgroudColor(0x00000000); language_cancel.setImageResource(R.drawable.canncel); lang_part1.addView(language_cancel, layout3); return language_bg; }