2016-08-03 19 views
0

嗨,朋友我想在我的列表視圖中的自定義字體和我使用自定義適配器爲此,但它不工作。任何人都可以幫助我。自定義字體文本視圖在listvliew android

{ 
     convertView = inflater.inflate(R.layout.listitem_row, null); 
     holder = new ViewHolder(); 
     holder.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1); 

     Typeface customfont = f1.showf(); 
     holder.txtViewTitle.setTypeface(customfont); 
     holder.txtViewDescription = (TextView) convertView.findViewById(R.id.textView2); 
     convertView.setTag(holder); 
    } 

f1.show代碼: -

public Typeface showf() 
    {    
     final AssetManager assets = this.getAssets(); 
     final Typeface tvFont = Typeface.createFromAsset(assets, "Monlam Uni OuChan2.ttf"); 

     return tvFont; 
    } 
+0

顯示您的持有人類? –

回答

1

確保你在你的文件名中的空格沒有阻止程序從正確識別該文件的事實。

0

嘗試改變/重構

Monlam統一OuChan2.ttf

monlam_uni_ouchan2.ttf

0

可以在CustomListAdapter類

private class CustomListAdapter extends ArrayAdapter { 

private Context mContext; 
private int id; 
private List <String>items ; 

public CustomListAdapter(Context context, int textViewResourceId , List<String> list) 
{ 
    super(context, textViewResourceId, list);   
    mContext = context; 
    id = textViewResourceId; 
    items = list ; 
} 

@Override 
public View getView(int position, View v, ViewGroup parent) 
{ 
    View mView = v ; 
    if(mView == null){ 
     LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     mView = vi.inflate(id, null); 
    } 

    TextView text = (TextView) mView.findViewById(R.id.textView); 

    if(items.get(position) != null) 
    { 
     text.setTextColor(Color.BLUE); 
     text.setText(items.get(position)); 
     text.setBackgroundColor(Color.RED); 
     int color = Color.argb(200, 255, 64, 64); 
      text.setBackgroundColor(color); 

    } 

    return mView; 
} 
有這

}

而且Custom_xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 
<TextView 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:id="@+id/textView" 
android:textSize="20px" 
android:paddingTop="10dip"  
android:paddingBottom="10dip"/> 
</LinearLayout> 
0

能否請您添加額外的細節和更多的記錄?

還要確保您的文件位於Assets文件夾中。不要只是自己添加它,使用文件>新建>文件夾>資產文件夾,如下圖所示。正如亞當指出的那樣,你也可以嘗試逃避空間角色。

Adding assets folder

0

自定義字體類

public class CustomTextView extends TextView { 

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

     applyCustomFont(context); 
    } 

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

     applyCustomFont(context); 
    } 

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

     applyCustomFont(context); 
    } 

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

list_item_xml

<RelativeLayout 
    android:id="@+id/itemContainer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <com.you.package.views.CustomTextView 
     android:id="@+id/name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@color/green_dark" 
     android:textSize="20sp" 
     android:text="Sample text" 
     android:layout_marginBottom="24dp"/> 

</RelativeLayout> 

在您的適配器觀點持有者

public static class ViewHolder extends RecyclerView.ViewHolder{ 
     private RelativeLayout itemContainer; 
     private final CustomTextView itemActName; 
     public ViewHolder(View v) { 
      super(v); 
      itemContainer = (RelativeLayout) v.findViewById(R.id.itemContainer); 
      itemActName = (CustomTextView) v.findViewById(R.id.name); 
     } 
    } 

@Override 
    public CustomAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View v = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.list_item_xml, parent, false); 

     return new ViewHolder(v); 
    } 



    @Override 
    public void onBindViewHolder(CustomAdapter.ViewHolder holder, final int position) { 

     holder.itemActName.setText(List.get(position).getText()); 
     holder.itemContainer.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if (mItemClickListener != null) { 
        mItemClickListener.onItemClick(v, position); 
       } 
      } 
     }); 
    } 
0

試試這個代碼可以幫助你

Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/abc.ttf"); 
    textView.setTypeface(tf); 

你需要把活動的上下文之前getAssets()

我的意思不是這樣的:

 final AssetManager assets = this.getAssets(); 

使用

​​
0

你在哪裏把你的字體?我可以看到文件名包含白色空白,這可能會導致您的問題。 我有另一種方式讓你做到這一點。 創建一個自定義的TextView類象下面這樣:

public class MyTextView extends TextView { 

    public MyTextView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(); 
    } 

    public MyTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public MyTextView(Context context) { 
     super(context); 
     init(); 
    } 

    private void init() { 
     if (!isInEditMode()) { 
      Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/MavenPro-Regular.ttf"); // make sure your font is put on assets/font location. 
      setTypeface(tf); 
     } 
    } 
} 

,然後只用它從XML這樣的:

<yourPackageName.MyTextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textSize="12dp" 
      android:text="REGULAR" 
      android:textColor="#868688"/> 
0

不要再次添加字體,當畫面爲null僅在getView()再次添加一次。

如圖所示,首先在assest文件夾中添加字體。

enter image description here

此層次結構將成爲路徑 「資產產生/字體/ bohema.otf」

@Override 
    public View getView(final int position, View convertView, final ViewGroup parent) { 
     View row = convertView; 
     ViewHolder holder; 

     if (convertView == null) { 
      row = inflater.inflate(R.layout.font_lv_item, null); 

      holder = new ViewHolder(); 
      holder.title = (TextView) row.findViewById(R.id.font_title_txt); 
      Typeface tf = Typeface.createFromAsset(mContext.getAssets(), "fonts/bohema.otf"); 
      holder.title.setTypeface(tf); 

      row.setTag(holder); 
     } else { 
      holder = (ViewHolder) row.getTag(); 
     } 

     Fonts item = data.get(position); 
     holder.title.setText(item.getTile()); 
     holder.title.setTypeface(item.getTf()); 

     return row; 
    } 
0

移動這一行字樣CustomFont的= f1.showf();轉換爲適配器的構造函數,並在convertView爲null時在ui組件中僅設置一次字體。希望它能起作用。

:)GlbMP