xml布局
<LinearLayout
????android:id="@+id/home_frag_list_ll_container"
? ? android:layout_width="match_parent"
? ? android:layout_height="30dp"
? ? android:orientation="horizontal">
</LinearLayout>
//課程詳細內(nèi)容=================01/16========重要的點========
List kcts = courinfoBean.getKcts();
????????????????//動態(tài)添加TextView? for循環(huán)也有l(wèi)ambada表達式
kcts.stream().forEach(str->{
TextView textView =new TextView(activity);
? ????????????????? //添加文字
? ? textView.setText(str);
? ????????????????? //添加一些屬性 //圖標
? ? Drawable drawable =activity.getDrawable(R.mipmap.tubiao);
? ????????????????? //設(shè)置圖片的位置
? ? drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getMinimumHeight());
? ? textView.setCompoundDrawables(drawable,null,null,null);
? ????????????????? //圖標間距? 單位是像素
? ? textView.setCompoundDrawablePadding(10);
? ????????????????? //垂直居中
? ? textView.setGravity(Gravity.CENTER_VERTICAL);
? ? textView.setTextSize(11);
? ????????????? //省略號
? ? textView.setSingleLine();
? ? textView.setEllipsize(TextUtils.TruncateAt.END);
????????????????? ? //文字的margin,通過LayoutParams來設(shè)置
? ? LinearLayout.LayoutParams params =new
? ? ? ? ? ? LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
? ? ? ? ? ? ViewGroup.LayoutParams.WRAP_CONTENT);
? ? params.rightMargin=10;
? ? textView.setLayoutParams(params);
? ????????????????? //添加到容器
? ? listVH.homeFragListLlContainer.addView(textView);
});