2017-01-27 1 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

カスタマイズされたクラスを作成し、xmlコードのTextViewにアタッチするだけではどうでしょうか。私は別の方法で表示することができます –

+0

plzこれを試してみてくださいTypeface.createFromAsset(ctx.getAssets()、 "fshruti.ttf") –

+0

なぜ、あなたは 'listTitle.setTypeface(null、Typeface.BOLD); 'bold'を使わない?とにかく、あなたの問題はコンテキストで、 'parent.getContext()'が処理を行うかもしれません。 – Wizard

答えて

0

これを行うときには、アダプタの内側にあるので、あなたは、ビューのコンテキストを使用する必要があります。

あなたは、ビューのコンテキストを取得することができます。これに

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

view.getContext()

この行を置き換える

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

エラーを修正します。これが助けてくれることを願って、もしそうなら私に知らせてください。

+0

あなたはそれをタイプフェイスbold = Typeface.createFromAsset(getContext()。getAssets()、 "shruti"で説明できますか?ttf ");ありがとう –

0

これは私がそれを行う方法

STEP 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); 
    } 

} 

STEP 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; 
    } 
} 

STEP 3:

はその後最後に、あなたはあなたのTextViewに上のカスタマイズされたクラスを添付してください。この

<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" /> 

NOTEのようなあなたのxml項目クラスに:

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" /> 

それがうまく機能願っています。 これは確かな解決策です。

関連する問題