设置整个“活动”的字体设置



在android中,我如何一次为ENTIRE ActivityFragment设置字体颜色和字体样式,这样我就不必将其设置为每个TextView了?

这是我的自定义文本视图类,

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class CustomTextView extends TextView {
    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }
    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    public CustomTextView(Context context) {
        super(context);
        init();
    }
    public void init() {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
                "fonts/Montserrat-Regular.ttf");
setTextAppearance(getContext(), android.R.style.headerText); //Here is the code for adding textview style.
        setTypeface(tf, 1);
    }
}

在xml中,我使用了我的自定义文本视图,而不是defalut textview,

<com.sample.android.utils.CustomTextView
                    android:id="@+id/login_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginLeft="92dp"
                    android:clickable="true"
                    android:text="Log In"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@color/login_page_text_normal"
                    android:textStyle="bold" />

或者只需要设置textstyle意味着,在styles.xml中添加这段代码,

<style name="headerText">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#ffffff</item>
        <item name="android:gravity">center</item>
        <item name="android:textSize">8.39pt</item>
        <item name="android:textStyle">bold</item>
        <item name="android:shadowColor">#000000</item>
        <item name="android:shadowDx">1</item>
        <item name="android:shadowDy">1</item>
        <item name="android:shadowRadius">1</item>
        <item name="android:adjustViewBounds">true</item>
        <item name="android:singleLine">true</item>
    </style>

在你的xml布局中添加这个

<TextView
        android:id="@+id/header_text"
        style="@style/headerText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

在res->values文件夹中创建一个style.xml,并将条目放入

<?xml version="1.0" encoding="utf-8"?>
 resources>
  <style name="stylingtextview" parent="@android:style/TextAppearance.Medium">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColor">#00FF00</item>
    <item name="android:typeface">monospace</item>
</style>

然后在所有的文本视图中使用这个

 <TextView
 android:id="@+id/textview"
 style="@style/stylingtextview" />

最新更新