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);
本文介绍如何在Android中动态添加及调整TextView控件的大小。包括使用LayoutParams设置控件属性、将新创建的控件添加到指定布局中等关键步骤。
2311

被折叠的 条评论
为什么被折叠?



