listview Android中的自定义字体?

时间:2015-05-30 07:21:25

标签: android listview fonts

如何添加自定义字体? 这是我的代码:

public void run() {
    ListAdapter adapter = new SimpleAdapter(DiaListActivity.this, diaList, R.layout.list_item, new String[]{TAG_SRNO, TAG_NAME}, new int[]{R.id.srno, R.id.name});
    setListAdapter(adapter);
}

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

    In R.layout.list_item  layout file if you have any textview then you can set this below code::-

    <com.example.TextViewPlus
            android:id="@+id/textViewPlus1"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:text="@string/showingOffTheNewTypeface"
            foo:customFont="saxmono.ttf">
        </com.example.TextViewPlus>


    Put these class file into your package: 

    TextViewPlus.java

    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Typeface;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.widget.TextView;

    public class TextViewPlus extends TextView {
        private static final String TAG = "TextView";

        public TextViewPlus(Context context) {
            super(context);
        }

        public TextViewPlus(Context context, AttributeSet attrs) {
            super(context, attrs);
            setCustomFont(context, attrs);
        }

        public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            setCustomFont(context, attrs);
        }

        private void setCustomFont(Context ctx, AttributeSet attrs) {
            TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
            String customFont = a.getString(R.styleable.TextViewPlus_customFont);
            setCustomFont(ctx, customFont);
            a.recycle();
        }

        public boolean setCustomFont(Context ctx, String asset) {
            Typeface tf = null;
            try {
            tf = Typeface.createFromAsset(ctx.getAssets(), asset);  
            } catch (Exception e) {
                Log.e(TAG, "Could not get typeface: "+e.getMessage());
                return false;
            }

            setTypeface(tf);  
            return true;
        }

    }



attrs.xml: (in res/values)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TextViewPlus">
        <attr name="customFont" format="string"/>
    </declare-styleable>
</resources>

答案 1 :(得分:0)

我通常做的是,创建一个自定义TextView并将其设置为我的内容视图,

public class CustomTextView extends TextView {

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

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

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

    public void setTypeface(Typeface tf, int style) {
        if (style == Typeface.BOLD) {
            super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Regular.ttf"));
        } else {
            super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf"));
        }
    }
}

现在只需将常规TextView替换为自定义TextView,

<TextView
 android:id="@+id/R.id.srno"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 />

向,

 <com.example.CustomTextView
  android:id="@+id/R.id.srno"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  />

这将使用自定义TextView更改普通TextView,因此将添加字体。

注意:确保您创建了一个名为“ fonts ”的文件夹并将字体文件放在那里。

如果还可以通过添加一行来自定义和添加不同的字体,

e.g。如果你使用,

android:textStyle="bold"

它将设置字体类型Roboto-Regular.ttf。这是在CustomTextView类中定义的,

if (style == Typeface.BOLD) {
            super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Regular.ttf"));
        }

答案 2 :(得分:0)

得到了......

@Override
public View getView(int pos, View convertView, ViewGroup parent){
    View v = convertView;
    if(v == null) {
        LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.list_item, null);
    }

    HashMap<String,String> value = diaList.get(pos);

    TextView tv = (TextView)v.findViewById(R.id.name);
    Typeface custom_fontG = Typeface.createFromAsset(getAssets(), "fonts/oriya.ttf");
    tv.setTypeface(custom_fontG);
    tv.setText(value.get(TAG_NAME));

    TextView tv2 = (TextView)v.findViewById(R.id.srno);
    Typeface custom_fontH = Typeface.createFromAsset(getAssets(), "fonts/oriya.ttf");
    tv2.setTypeface(custom_fontH);
    tv2.setText(value.get(TAG_SRNO));

    return v;
}
};

// updating listview

setListAdapter(adapter);