您的位置:首页 > 编程语言 > PHP开发

设置控件的随机显示位置 setlayoutparams

2013-07-03 16:01 344 查看


设置控件的随机显示位置 setlayoutparams

分类: android初阶篇2010-12-06
10: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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: