以LinearLayout中的控件ImageView为例
LinearLayout layout = (LinearLayout) convertView.findViewById(R.id.linearlayout); ImageView imageView = new ImageView(this); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100, 100); params.setMargins(5, 5, 5, 5); layout.addView(imageView);
new LinearLayout.LayoutParams(100,100) 即设置高度和宽度
params.setMargins即设置上下左右四个Margin值,顺序为left, top, right, bottom
可以动态加载控件,也可以动态设置控件