[英]ImageView no margin when added to Relative Layout Dynamic
如果在我的相对布局中添加Imageview时遇到问题,如果将layoutparams添加到视图中,则不会添加imageview,但是如果imageview没有layoutparams,它将显示但没有图像。 Imageview是通过代码创建的。
TableLayout imageCon = (TableLayout)root.findViewById(R.id.home_screen_table_imagecon);
TableRow imageTableRow = new TableRow(root.getContext());
TableRow.LayoutParams rowSpanLayout = new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
imageTableRow.addView(getLove());
imageTableRow.addView(getHappy());
imageCon.addView(imageTableRow,rowSpanLayout);
imageCon.setShrinkAllColumns(true);
private ImageView getLove(){
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 10, 10,10);
imgLove.setLayoutParams(lp);
imgLove.setImageResource(R.drawable.img_emo_love);
return imgLove;
}
private ImageView getHappy(){
imgHappy.setImageResource(R.drawable.img_emo_happy);
return imgHappy;
}
我期待着您的帮助。
谢谢Chkm8
您需要使用TableRow.LayoutParams
,因为您将ImageView
添加到TableRow
将LayoutParams
更改为
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT);
lp.setMargins(10, 10, 10,10);
imgLove.setLayoutParams(lp);
如果将ImageView
添加到RelativeLayout
则使用RelativeLayout.LayoutParams
如果将ImageView
添加到LinearLayout
则使用LinearLayout.LayoutParams
放在xml ImageView标记的行下面
android:adjustViewBounds="true"
请享用...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.