设置整个活动的字体设置

时间:2013-12-03 08:27:27

标签: android android-fragments android-activity textview android-fonts

在android中,如何一次性设置整个ActivityFragment的字体颜色和字体样式,因此我不必将其设置为每个TextView }?

2 个答案:

答案 0 :(得分:2)

这是我的自定义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中,我使用自定义textview而不是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"
        />

答案 1 :(得分:0)

在res-&gt; 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" />
相关问题