从 CustomTextView 更改颜色文本

Change color Text from CustomTextView

默认情况下,以编程方式更改 textColor 是:

textView.setTextColor(Color.RED);

我需要一个自定义 Textview 来默认更改字体和颜色,如何从 CustomTextView class 更改文本颜色,这是我的代码。

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(!isInEditMode()) {
        if (style == Typeface.BOLD) {
            super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Lato-Bold.ttf"));
        } else if(style == Typeface.ITALIC){  // constant used to set Lato-Light.
            super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Lato-Light.ttf"));
        }else {
            super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Lato-Regular.ttf"));
        }
    }
}

在每个构造函数中的每个超级后使用 setTextColor(Color.RED);

下面的代码是设置默认文本颜色和字体的方法。

public class CustomTextView extends TextView {
public CustomTextView(Context context) {
    super(context);
    init(context);
}

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

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

private void init(Context context) {
    setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/Lato-Light.ttf"));
    setTextColor(Color.RED);
}
}

每次创建文本视图时都会调用 init() 方法,然后会在其中设置字体和颜色。您可以在那里操纵任何其他您想要的变量。

步骤 1

在 /assets 目录(不是 /resource 目录)中,创建一个名为 /fonts 的文件夹。在此处复制您的自定义字体。您可以同时使用 TTF 和 OTF 字体。

步骤 2

在 /res/values 文件夹中,创建一个名为 attrs.xml 的新文件。这就是 Android SDK 让您为小部件命名自定义属性的方式。

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

步骤 3

在 /res/layouts 中,您需要将要创建的自定义文本视图包含在 activity_main.xml 文件中。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:customfontdemo="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:padding="12dp"
        android:text="Standard Android Font" />
    
     <com.authorwjf.customfontdemo.MyTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="32sp"
        android:padding="12dp"
        customfontdemo:fontName="pipe_dream.ttf"
        android:text="Custom Android Font" />

</LinearLayout>

步骤 4

在 /src 文件夹中,您需要创建 MyTextView class。它扩展了标准文本视图,从自定义属性中提取字体名称,并应用字体。

package com.authorwjf.customfontdemo;

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

public class MyTextView extends TextView {

    public MyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }
    
    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
        
    }
    
    public MyTextView(Context context) {
        super(context);
        init(null);
    }
    
    private void init(AttributeSet attrs) {
        setTextColor(Color.RED);
        if (attrs!=null) {
             TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.MyTextView);
             String fontName = a.getString(R.styleable.MyTextView_fontName);
             if (fontName!=null) {
                 Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/"+fontName);
                 setTypeface(myTypeface);
             }
             a.recycle();
        }
    }

}

步骤 5

因为文本视图现在是独立的,所以您不需要对我们的 /src/MainAcitivity.java 文件进行任何修改。

package com.authorwjf.customfontdemo;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

}