Android可扩展列表查看项目字体样式

时间:2013-11-28 14:35:57

标签: android list listview typeface

我已按照以下链接提供的教程:

http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/

如何在list_group.xml和list_item.xml中为textviews提供自定义字体(字体)。

我已将Arial.ttf文件复制到资源文件夹中。

请帮帮我。

2 个答案:

答案 0 :(得分:1)

您可以通过覆盖TextView类来创建自定义textview,然后在xml中使用CustomTextView而不是使用TextView:

    public class CustomTextView extends TextView {

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

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

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

private void init(Context context, AttributeSet attrs) {

}

@Override
public void setTypeface(Typeface tf, int style) {
    if (!isInEditMode()) {
    Typeface normalTypeFace = Typeface.createFromAsset(getContext()
            .getAssets(), "Esans.ttf");
    Typeface boldTypeFace = Typeface.createFromAsset(getContext()
            .getAssets(), "EsansBol.ttf");
    Typeface lightTypeFace = Typeface.createFromAsset(getContext()
            .getAssets(), "EsansLig.ttf");
    Typeface mediumTypeFace = Typeface.createFromAsset(getContext()
            .getAssets(), "EsansMed.ttf");
    //Modifying styles to support the different styles of custom font
        switch (style) {
        case Typeface.NORMAL:
            super.setTypeface(normalTypeFace);
            break;
        case Typeface.BOLD:
            super.setTypeface(boldTypeFace);
            break;
        case Typeface.ITALIC:
            super.setTypeface(lightTypeFace);
            break;
        case Typeface.BOLD_ITALIC:
            super.setTypeface(mediumTypeFace);
            break;
        default:
            super.setTypeface(normalTypeFace);
            break;

        }
    }
}

}

答案 1 :(得分:1)

您可以尝试在xml中添加字体

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

}

你的xml中的

    <com.icl.examples.TextViewPlus
    android:id="@+id/textViewPlus1"
    android:layout_height="match_parent"
    android:layout_width="match_parent" 
    android:text="@string/info_text"
    android:textColor="@color/White"
    android:textStyle="bold"
    android:textSize="50sp"
    foo:customFont="Bamini.ttf">
</com.icl.examples.TextViewPlus>

将此添加到资源,res-&gt; values-attrs.xml

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