1:动态添加控件
/ 控件需要新创建 new 出来
TextView tVote = new TextView(mContext);
tVote.setText((char)( 65 + i) + ": " + votes[i]);
tVote.setTextColor(Color.parseColor("#444444"));
// setTextSize() 里面的单位是sp
tVote.setTextSize(14);
// 父控件是LinearLayout 若是相对布局用RelativeLayout.LayoutParams
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
// 需要 dp 转成像素单位
linearParams.bottomMargin = Constants.dip2px(mContext,8);
tVote.setLayoutParams(linearParams);
holder.event_ll_layout.addView(tVote);
2: 动态设置控件的大小
// 控件已经存在,获取控件的 属性
RelativeLayout.LayoutParams linearParams = (RelativeLayout.LayoutParams) holder.mView.getLayoutParams();
linearParams.width = 200;
holder.mView.setBackgroundColor(Color.parseColor("#ff0000"));
holder.mView.setLayoutParams(linearParams);