1

Androidでは、フォントの色とフォントスタイルを全体ActivityまたはFragment一度に設定するにはどうすればよいので、それぞれに設定する必要はありませんTextViewか?

4

2 に答える 2

2

これは私のカスタム テキストビュー クラスです。

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 の代わりにカスタムの 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"
        />
于 2013-12-03T08:37:37.390 に答える