自定义字体Text-View在android中的listvliew中

时间:2016-08-03 05:25:08

标签: android listview custom-adapter

朋友们,我想在我的列表视图中使用自定义字体,我使用自定义适配器,但它无法正常工作。任何人都可以帮助我。

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

9 个答案:

答案 0 :(得分:1)

确保文件名中包含空格这一事实并不妨碍程序正确识别文件。

答案 1 :(得分:0)

尝试更改/重构

Monlam Uni OuChan2.ttf

<强> monlam_uni_ouchan2.ttf

答案 2 :(得分: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>

答案 3 :(得分:0)

您能否添加额外的详细信息和更多日志记录?

还要确保您的文件位于Assets文件夹中。不要只是自己添加,使用文件&gt;新建&gt;文件夹&gt;资产文件夹,如下图所示。正如亚当指出的那样,你也可以尝试逃避空间角色。

Adding assets folder

答案 4 :(得分: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);
                }
            }
        });
    }

答案 5 :(得分:0)

试试这段代码可以帮到你

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

您需要在getAssets()

之前添加活动的上下文

我的意思是代替:

      final AssetManager assets = this.getAssets();

使用

      Context context;
      final AssetManager assets = context.getAssets();

答案 6 :(得分: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"/>

答案 7 :(得分:0)

不要再次添加字体,只在null中只有getView()次观看时添加一次。

首先在assest文件夹中添加字体,如图所示。

enter image description here

这个层次结构将成为路径&#34; assests / fonts / bohema.otf&#34;

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

答案 8 :(得分:0)

移动此行字体customfont = f1.showf();当convertView为null时,转换器的构造函数和在ui组件中只设置一次字体。希望它会奏效。

:) GlbMP