android 在代码中设置视图布局和组件


1,LinnerLayout布局的创建和属性设置,并往里面添加子控件。

linearLayout = new LinearLayout(context);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
linearLayout.setBackgroundColor(0x00000000);
linearLayout.setOrientation(LinearLayout.VERTICAL);

textView = new TextView(context);
textView.setText("");
LinearLayout.LayoutParams layoutParams_tv = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, margins);
layoutParams_tv.gravity=Gravity.BOTTOM;
linearLayout.addView(textView,layoutParams_tv);

2,RelativeLayout布局的创建和属性设置,并往里面添加子控件。

//父控件
reLayout = new RelativeLayout(context);
//半透明e0000000
reLayout.setBackgroundColor(0x00000000);
reLayout.setAlpha(100);
RelativeLayout.LayoutParams mLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT); ImgUrl.setLayoutParams(mLayoutParams); ImgUrl.setScaleType(ScaleType.FIT_CENTER); ImgUrl.setBackgroundResource(0); ImgUrl.setImageBitmap(bitmapBanner);
ImgUrl.setId(102);
reLayout.addView(ImgUrl);
txtClose = new ImageView(context); RelativeLayout.LayoutParams mLayoutParams1 = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); mLayoutParams1.addRule(RelativeLayout.ALIGN_TOP, ImgUrl.getId()); mLayoutParams1.addRule(RelativeLayout.ALIGN_RIGHT, ImgUrl.getId()); txtClose.setImageBitmap(BitmapFactory.decodeByteArray(closeByte, 0, closeByte.length)); txtClose.setScaleType(ScaleType.FIT_CENTER); txtClose.setId(100); if(type == Configuration.ORIENTATION_LANDSCAPE){ mLayoutParams1.width = ScreenUtil.dip2px(context, 30); mLayoutParams1.height= ScreenUtil.dip2px(context, 30); }else if (type == Configuration.ORIENTATION_PORTRAIT){ mLayoutParams1.width = ScreenUtil.dip2px(context, 35); mLayoutParams1.height= ScreenUtil.dip2px(context, 35); } reLayout.addView(txtClose,mLayoutParams1);

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM