2017-01-27 10 views
0

Я пытаюсь установить внешний шрифт в одном из моих расширяемых видов списка. Я пытаюсь как нижеВнешний шрифт в ExpandableList

public View getGroupView(int groupPosition, boolean isExpanded, 
         View convertView, ViewGroup parent) { 
    String headerTitle = (String) getGroup(groupPosition); 
    String headerTitleDate = (String) getGroupD(groupPosition); 

    if (convertView == null) { 
     LayoutInflater layoutInflater = (LayoutInflater) this._context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = layoutInflater.inflate(R.layout.list_group, null); 

    } 

    Typeface bold = Typeface.createFromAsset(getContext().getAssets(), "shruti.ttf"); 
    TextView listTitle = (TextView) convertView 
      .findViewById(R.id.tv_listtitle); 
    TextView listTitleDate = (TextView) convertView 
      .findViewById(R.id.tv_date); 
    listTitle.setTypeface(null, Typeface.BOLD); 

    listTitle.setText(headerTitle); 

    listTitleDate.setTypeface(null, Typeface.BOLD); 

    listTitleDate.setText(headerTitleDate); 

    return convertView; 
} 

Но я получаю getAssets() не может решить. Я пытался использовать его с Context и без него, но не с успехом. Кто-нибудь может предложить мне, что с ним не так?

Благодаря

+0

Почему бы вам просто не создать индивидуальный класс и просто прикрепить его к TextView в xml-коде. Я могу показать наоборот –

+0

PLZ попробуйте этот Typeface.createFromAsset (ctx.getAssets(), "fshruti.ttf") –

+0

Удивление, почему вы используете 'listTitle.setTypeface (null, Typeface.BOLD);' вместо не использовать 'bold'? В любом случае ваша проблема - это контекст, тогда 'parent.getContext()' может выполнять эту работу. – Wizard

ответ

0

Вы должны использовать контекст мнение, когда делать это, так как он находится внутри адаптера.

Вы можете получить контекст мнение, путем: view.getContext()

Замена этой линии:

Typeface bold = Typeface.createFromAsset(getContext().getAssets(), "shruti.ttf"); 

с этим:

Typeface bold = Typeface.createFromAsset(convertView.getContext().getAssets(), "shruti.ttf"); 

будет исправить ошибку. Надеюсь, это поможет, дайте мне знать, если это произойдет.

+0

Не могли бы вы объяснить это с помощью строки Typeface bold = Typeface.createFromAsset (getContext(). GetAssets(), "shruti.ttf "); –

0

Это, как я это сделать

ШАГ 1:

Я первый сделать класс с customizable шрифта, для вашего случая вы используете shruti.ttf

public class Shruti extends TextView{ 




    public Champagne(Context context) { 
     super(context); 

     applyCustomFont(context); 
    } 

    public Champagne(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     applyCustomFont(context); 
    } 

    public Champagne(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 

     applyCustomFont(context); 
    } 

    private void applyCustomFont(Context context) { 
     Typeface customFont = FontCache.getTypeface("shruti.ttf", context); 
     setTypeface(customFont); 
    } 

} 

ШАГ 2:

Затем вы также включаете FontCache класс

public class FontCache { 


    private static HashMap<String, Typeface> fontCache = new HashMap<>(); 

    public static Typeface getTypeface(String fontname, Context context) { 
     Typeface typeface = fontCache.get(fontname); 

     if (typeface == null) { 
      try { 
       typeface = Typeface.createFromAsset(context.getAssets(), fontname); 
      } catch (Exception e) { 
       return null; 
      } 

      fontCache.put(fontname, typeface); 
     } 

     return typeface; 
    } 
} 

ШАГ 3:

Затем, наконец, прикрепить настроенный класс к вашему TextView. в классе xml пункта как этого

<company.override.huzykamz.pixsar.customization.Shruti 
      android:layout_width="match_parent" 
      android:id="@+id/post_desc" 
      android:layout_height="wrap_content" 
      android:text="" 
      android:textColor="#000" 
      android:textSize="15dp" 
      android:paddingLeft="15dp" 
      android:paddingRight="15dp" 
      android:paddingBottom="15dp" /> 

ПРИМЕЧАНИЕ:

company.override.huzykamz.pixsar.customization is just a package name example , but you just put yours. 

Вместо

<TextView 
      android:layout_width="match_parent" 
      android:id="@+id/post_desc" 
      android:layout_height="wrap_content" 
      android:text="" 
      android:textColor="#000" 
      android:textSize="15dp" 
      android:paddingLeft="15dp" 
      android:paddingRight="15dp" 
      android:paddingBottom="15dp" /> 

Надеется, что это работает хорошо. Это верное решение для решения

 Смежные вопросы

  • Нет связанных вопросов^_^