【问题标题】:Android expandable list view items font styleAndroid 可扩展列表视图项目字体样式
【发布时间】:2013-11-28 14:35:57
【问题描述】:

我已按照以下链接中的教程进行操作:

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

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

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

请帮帮我。

【问题讨论】:

    标签: android list listview typeface


    【解决方案1】:

    您可以像我在这里所做的那样创建自定义文本视图,方法是覆盖 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;
    
            }
        }
    }
    

    }

    【讨论】:

    • 在java代码中提到的例子中无论如何要改变字体
    • 当这是一种简单的方法时,您为什么要这样做?但是可以,yourTextView.setTypeFace(TypeFace.createFromAsset(Assetmanager mgr, String path);
    • Pontus.... 你能编辑你的代码吗?我只想使用 arial.ttf。请上传如何在 xml 中使用自定义 textview。
    【解决方案2】:

    你可以试试这个在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->values-attrs.xml

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

    【讨论】:

    • 嗨 Boopathi... 抱歉回复晚了。我想分配 arail 字体。所以我必须在 xml 中给出 foo:customFont="Arial.ttf" 。我说的对吗?
    • Boopathi... 我已经尝试过上面的代码。但我在
    • @user3021918 com.icl.examples 是 TextViewPlus 类的包名称。所以将 packageName 更改为您的 TextViewPlus 所在的位置
    • 我只在那里给出了我的包名。即使我遇到了错误。
    • @user3021918 在您的 xml 文件顶部,您需要指定 xmlns:foo="schemas.android.com/apk/res-auto"。您应该在顶部看到类似 xmlns:android 的内容。
    猜你喜欢
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多