2013-01-25 46 views
1

我試圖在自定義可擴展列表視圖中使用自定義字體。但是,當我嘗試這樣做時,最初,當Viewpager片段(我使用它)加載時,文本和字體對於ExpandableListView不可見。自定義ExpandableListAdapter中的Android自定義字體

一旦我點擊可展開列表視圖中的空白文本,就會顯示文本和字體。這個問題似乎只發生在自定義字體上。如果我使用任何其他自定義功能,如更改textview的顏色等,它的效果很好。好心幫

請參閱進一步參考所附的圖片: 1 2

我的代碼自定義擴展的listadapter

相關部分:

public class MyExpandableListAdapter extends SimpleExpandableListAdapter { 

    View cntView; 
    LayoutInflater inflater; 
    Context cxt; 
    TextView tvGrp, tvChild; 

    public MyExpandableListAdapter(Context context, 
      List<? extends Map<String, ?>> groupData, int groupLayout, 
      String[] groupFrom, int[] groupTo, 
      List<? extends List<? extends Map<String, ?>>> childData, 
      int childLayout, String[] childFrom, int[] childTo) { 
     super(context, groupData, groupLayout, groupFrom, groupTo, childData, 
       childLayout, childFrom, childTo); 
     cxt=context; 
    } 

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, 
      View convertView, ViewGroup parent) { 

     super.getGroupView(groupPosition, isExpanded, convertView, parent); 
     View tmp; 


     if (convertView == null) { // if it's not recycled, initialize some attributes 
      tmp=new View(cxt); 
      inflater=(LayoutInflater)cxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      tmp=inflater.inflate(R.layout.explistgroup, null); 
     }else{ 
      tmp = (View)convertView; 
     } 

     tvGrp=(TextView) tmp.findViewById(R.id.rowTextView); 
     tvGrp.setTypeface(FontLoader.loadTypeface(cxt,R.raw.roboto_regular)); // This does not work. It shows a blank textview initially. Only after I click it, the typeface and the text come into effect 
     //tvGrp.setText("ambit"); -- This works well 

     return tmp; 

    } 

回答

0

嘗試從資產自定義字體加載,例如:

Typeface tf = Typeface.createFromAsset(getAssets(), "<your font name>.ttf"); 
tvGrp.setTypeface(tf); 
+0

感謝納塔利,但那並沒有幫助我。我遇到同樣的問題:(。請讓我知道,如果你能想出任何其他方式來解決這個問題。 – ambit

0

確定這裏是你需要的東西更多信息: 由1-從那裏你正在使用的自定義適配器爲您的列表視圖擴張活動初始化一個公共靜態變量說MainListActivity這樣的:

public static Typeface typeFace;

2 - 現在初始化它就像納塔利在同一提到活動是這樣的:

tf = Typeface.createFromAsset(getAssets(), "<your font name>.ttf");

3-現在使用您的自定義適配器以下爲導演納塔利先生但是稍有變化

tvGrp.setTypeface(MainListActivity.typeFace);

如果問題仍然存在也嘗試與BaseExpandableListAdapter

0

擴展自定義適配器類你只創建一個視圖,而不是設置文本。在xml中是否有text? 我覺得這個方法應該是這樣的:

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, 
     View convertView, ViewGroup parent) { 

    ViewGroup tmp = (ViewGroup)super.getGroupView(groupPosition, isExpanded, convertView, parent); 
    tvGrp=(TextView) tmp.findViewById(R.id.rowTextView); 
    tvGrp.setTypeface(FontLoader.loadTypeface(cxt,R.raw.roboto_regular)); // This does not work. It shows a blank textview initially. Only after I click it, the typeface and the text come into effect 
    //tvGrp.setText("ambit"); -- This works well 

    return tmp; 

} 

或者你創建和初始化的,而不是調用超。

而且這應該正常工作。