1

在android中,我怎样才能为整个ActivityFragment一次设置字体颜色和字体样式,所以我不必将它设置为每一个TextView

4

2 回答 2

2

This is my custom textview class,

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

    }

}

In xml, i used my custom textview instead of 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" />

or you need to set only textstyle means, in you styles.xml add this piece of code,

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

and in your xml layout add this

<TextView
        android:id="@+id/header_text"
        style="@style/headerText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
于 2013-12-03T08:37:37.390 回答
0

在您的 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" />
于 2013-12-03T08:41:52.840 回答