我在运行时将一个TextView添加到LinearLayout
我想在我的assets/fonts/
文件夹中设置运行时的字体
我已经完成了以下操作,但它无法正常工作:
private ArrayList<TextView> getExperTextViews() {
ArrayList<TextView> textViewArrayList = new ArrayList<>();
TextView tv_title = new TextView(DesignerDetailActivity.this);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
parms.setMargins(0,0,0,5);
tv_title.setLayoutParams(parms);
Typeface font=Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Bold.ttf");
tv_title.setTypeface(font);
tv_title.setText("EXPERTISE");
tv_title.setTextSize(16);
textViewArrayList.add(tv_title);
ArrayList<DesignerExpertise> designerExpertiseArrayList = designer.getExpertiseArrayList();
String expertiseString = "<font color='#a8a8a8'>{property}</font> ";
for (int i = 0; i < designerExpertiseArrayList.size(); i++) {
TextView tv = new TextView(DesignerDetailActivity.this);
tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
DesignerExpertise designerExpertise = designerExpertiseArrayList.get(i);
ArrayList<DesignerExpertiseValue> designerExpertiseValueArrayList = designerExpertise.getDesignerExpertiseValues();
String newExpertiseString = expertiseString.replace("{property}", designerExpertise.getProperty().toUpperCase());
newExpertiseString = newExpertiseString + designerExpertiseValueArrayList.get(0).getName();
for (int j = 1; j < designerExpertiseValueArrayList.size(); j++) {
newExpertiseString = newExpertiseString + " ," + designerExpertiseValueArrayList.get(1).getName();
}
Typeface _font=Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Regular.ttf");
tv_title.setTypeface(_font);
tv.setText(Html.fromHtml(newExpertiseString), TextView.BufferType.SPANNABLE);
tv.setTextSize(16);
tv.setPadding(0,5,0,5);
textViewArrayList.add(tv);
}
return textViewArrayList;
}
请帮助我。
答案 0 :(得分:0)
我认为这是一个复制/粘贴错误。在for
循环中,代码应为tv.setTypeface(_font)
,而不是tv_title.setTypeface(_font)
for (int i = 0; i < designerExpertiseArrayList.size(); i++) {
TextView tv = new TextView(DesignerDetailActivity.this);
tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
DesignerExpertise designerExpertise = designerExpertiseArrayList.get(i);
ArrayList<DesignerExpertiseValue> designerExpertiseValueArrayList = designerExpertise.getDesignerExpertiseValues();
String newExpertiseString = expertiseString.replace("{property}", designerExpertise.getProperty().toUpperCase());
newExpertiseString = newExpertiseString + designerExpertiseValueArrayList.get(0).getName();
for (int j = 1; j < designerExpertiseValueArrayList.size(); j++) {
newExpertiseString = newExpertiseString + " ," + designerExpertiseValueArrayList.get(1).getName();
}
Typeface _font=Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Regular.ttf");
tv.setTypeface(_font);
tv.setText(Html.fromHtml(newExpertiseString), TextView.BufferType.SPANNABLE);
tv.setTextSize(16);
tv.setPadding(0,5,0,5);
textViewArrayList.add(tv);
}