2015-11-17 111 views
0

我在運行時將一個TextView添加到LinearLayout。
我想在運行時從我的assets/fonts/文件夾設置字體。
我也做了以下內容,但它不工作:設置自定義字體動態添加文本視圖android

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> &nbsp;"; 
    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

我認爲這是一個複製/粘貼錯誤。在你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); 
} 
+0

感謝我的錯誤.... :( –