android动态添加布局时,布局参数不生效
如下代码:@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedIns...
如下代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.width = 200;
params.height = 200;
params.topMargin = 200;
params.leftMargin = 200;
ImageView view = new ImageView(getApplicationContext());
view.setBackgroundResource(R.drawable.ic_launcher);
addContentView(view, params);
}
在一个新activity中,动态添加一个imageview,但是parms的topMargin,leftMargin并没有生效,显示的效果是imageview依然在activity的左上角。
请问是什么原因?谢谢 展开
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.width = 200;
params.height = 200;
params.topMargin = 200;
params.leftMargin = 200;
ImageView view = new ImageView(getApplicationContext());
view.setBackgroundResource(R.drawable.ic_launcher);
addContentView(view, params);
}
在一个新activity中,动态添加一个imageview,但是parms的topMargin,leftMargin并没有生效,显示的效果是imageview依然在activity的左上角。
请问是什么原因?谢谢 展开
展开全部
增加父类布局,即要添加到的布局,如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rl = (RelativeLayout)findViewById(R.id.rl);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.width = 200;
params.height = 200;
params.topMargin = 200;
params.leftMargin = 200;
ImageView view = new ImageView(getApplicationContext());
view.setBackgroundResource(R.drawable.ic_launcher);
rl.addView(view, params);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rl = (RelativeLayout)findViewById(R.id.rl);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.width = 200;
params.height = 200;
params.topMargin = 200;
params.leftMargin = 200;
ImageView view = new ImageView(getApplicationContext());
view.setBackgroundResource(R.drawable.ic_launcher);
rl.addView(view, params);
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
大雅新科技有限公司
2024-11-19 广告
2024-11-19 广告
这方面更多更全面的信息其实可以找下大雅新。深圳市大雅新科技有限公司从事KVM延长器,DVI延长器,USB延长器,键盘鼠标延长器,双绞线视频传输器,VGA视频双绞线传输器,VGA延长器,VGA视频延长器,DVI KVM 切换器等,优质供应商,...
点击进入详情页
本回答由大雅新科技有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |