设置控件的随机显示位置 setlayoutparams
2013-07-03 16:01
344 查看
设置控件的随机显示位置 setlayoutparams
分类: android初阶篇2010-12-0610:08 3989人阅读 评论(0) 收藏 举报
RelativeLayout.LayoutParams parms=(RelativeLayout.LayoutParams)img.getLayoutParams();
parms.leftMargin = (int) (Math.random() * 320);
parms.topMargin = (int) (Math.random() * 480);
img.setLayoutParams(parms);
img.invalidate();
[java] view
plaincopy
private RelativeLayout.LayoutParams getOtherSetLayoutParameter() {
int height =190;
int width = 310;
DisplayMetrics dm=getResources().getDisplayMetrics();
if(dm.densityDpi == DisplayMetrics.DENSITY_HIGH){
height = height*DisplayMetrics.DENSITY_HIGH/DisplayMetrics.DENSITY_MEDIUM;
width = width*DisplayMetrics.DENSITY_HIGH/DisplayMetrics.DENSITY_MEDIUM;
}
else if(dm.densityDpi == DisplayMetrics.DENSITY_LOW){
height = height*DisplayMetrics.DENSITY_LOW/DisplayMetrics.DENSITY_MEDIUM;
width = width*DisplayMetrics.DENSITY_LOW/DisplayMetrics.DENSITY_MEDIUM;
}
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(width, height);
int screenWidth=dm.widthPixels;
int screenHeight=dm.heightPixels;
lp.topMargin = screenHeight/2-height/2;
lp.leftMargin = screenWidth/2-width/2;
return lp;
}
XXX.addView(mOthersetModeLayout,getOtherSetLayoutParameter());
把上面的代码精简一下:
private RelativeLayout.LayoutParams getOtherSetLayoutParameter() {
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(310, 248);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
return lp;
}
相关文章推荐
- 设置控件的随机显示位置 setlayoutparams
- 设置控件的随机显示位置 setlayoutparams
- javascript与jQuery设置取得div绝对位置一个小应用(像日历控件一样,在编辑框下面显示一个层)
- javascript与jQuery设置取得div绝对位置一个小应用(像日历控件一样,在编辑框下面显示一个层)
- 3013-12-11在OnSize中设置好控件的位置后,初始化的时候没有正常显示问题
- android 获取控件大小和设置调整控件的位置XY
- 自定义Dialog设置显示位置
- 动态代码中设置控件相对位置
- 蜗牛爱课 -- iOS 设置UIButton的字体的大小、显示位置、大小
- 如何设置和获得控件相对于Canvas的位置
- 控制控件在页面的显示位置
- activity设置成dialog后,再设定其显示位置和高宽
- 设置UIButton的文字显示位置、字体的大小、字体的颜色
- C#使用chart控件设置Series名称显示的位置
- iOS开发中的错误整理,(百思项目,指示器位置)设置控件尺寸和点坐标,先设置尺寸,再设置点坐标
- actionMode - 在屏幕中的显示位置设置
- android编程中setLayoutParams方法设置
- Android 自定义Dialog背景透明及显示位置设置
- 使用WindowManager.LayoutParams的x和y设置窗体位置
- 单片机==lcd_显示字符_可设置位置(22)