10

私の PreferenceActivity は次のようになります。

import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.widget.TextView;

public class Settings extends PreferenceActivity {

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
            super.onCreate(savedInstanceState);

            //setContentView(R.layout.about);

            Typeface timesFont = Typeface.createFromAsset(getAssets(), "fonts/times.ttf");

            TextView about_txt = (TextView) findViewById(R.id.about_txt);        
            about_txt.setTypeface(timesFont);
            about_txt.setText(R.string.about_txt);


            if (Build.VERSION.SDK_INT<Build.VERSION_CODES.HONEYCOMB)
            {
                addPreferencesFromResource(R.xml.preferences);
            } else {
                // Display the fragment as the main content.
                getFragmentManager().beginTransaction().replace(android.R.id.content,
                        new PrefsFragment()).commit();
            }
    }

    public static class PrefsFragment extends PreferenceFragment 
    {
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                // Load the preferences from an XML resource
                addPreferencesFromResource(R.xml.preferences);
            }
        }
}

そしてpreference.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

        <ListPreference
            android:dialogTitle="@string/size_dialog"
            android:entries="@array/size_list"
            android:entryValues="@array/entry_size"
            android:key="size_list"
            android:summary="@string/size_summary"
            android:title="@string/pref4title"
            />

    </PreferenceCategory>

    <PreferenceCategory
        android:title="@string/pref_about"
        android:layout="@layout/about"
        >

    </PreferenceCategory>

</PreferenceScreen>

@layout/about (about.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"> 

          <TextView
            android:id="@+id/about_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="some text here"   
            />

</LinearLayout>

私は特定の言語を持っているので、about.xml のテキストには特定のフォントを使用する必要があります。私が使用するTextViewのために定期的に:

 Typeface timesFont = Typeface.createFromAsset(getAssets(), "fonts/times.ttf");

 TextView about_txt = (TextView) findViewById(R.id.about_txt);        
 about_txt.setTypeface(timesFont);
 about_txt.setText(R.string.about_txt);

しかし、設定画面に追加する about.xml のテキストにアクセスするには、何を使用する必要がありますか?

前もって感謝します!

4

4 に答える 4

6

独自の設定を記述して xml に追加する方が簡単かもしれません。設定フィールドに Spannable を介してカスタム フォントを
設定するだけです:(長く見えますが、すばやく完了します:))

完全なソリューション:

private void convertPreferenceToUseCustomFont(Preference somePreference) {
    CustomTypefaceSpan customTypefaceSpan = new CustomTypefaceSpan("", net.mikekober.myMory.utils.Utils.getUsedTypeFace(getActivity()));

    SpannableStringBuilder ss;
    if (somePreference.getTitle() != null) {
        ss = new SpannableStringBuilder(somePreference.getTitle().toString());
        ss.setSpan(customTypefaceSpan, 0, ss.length(),Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        somePreference.setTitle(ss);
    }

    if (somePreference.getSummary() != null) {
        ss = new SpannableStringBuilder(somePreference.getSummary().toString());
        ss.setSpan(customTypefaceSpan, 0, ss.length(),Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        somePreference.setSummary(ss);
    }
}

すべての設定に対してそれを呼び出すと、完了です:)

楽しんでください && 幸運を

ところで:テキストを変更したら、もう一度呼び出してください!!
btw2: チェック、表示、適用、改善 ;) から: https://stackoverflow.com/a/10741161/371749

ところで、ほとんど忘れていました:

static private class CustomTypefaceSpan extends TypefaceSpan {

    private final Typeface newType;

    public CustomTypefaceSpan(String family, Typeface type) {
        super(family);
        newType = type;
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        applyCustomTypeFace(ds, newType);
    }

    @Override
    public void updateMeasureState(TextPaint paint) {
        applyCustomTypeFace(paint, newType);
    }

    private static void applyCustomTypeFace(Paint paint, Typeface tf) {
        int oldStyle;
        Typeface old = paint.getTypeface();
        if (old == null) {
            oldStyle = 0;
        } else {
            oldStyle = old.getStyle();
        }

        int fake = oldStyle & ~tf.getStyle();
        if ((fake & Typeface.BOLD) != 0) {
            paint.setFakeBoldText(true);
        }

        if ((fake & Typeface.ITALIC) != 0) {
            paint.setTextSkewX(-0.25f);
        }

        paint.setTypeface(tf);
    }
}
于 2014-03-04T13:27:47.540 に答える
4
public class CustomPreference extends Preference 
    implements PreferenceStyle {

    private int style;

    public CustomPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }

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

    public CustomPreference(Context context) {
        super(context);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        switch (style) {
        case STYLE_ALARMED:
            setStyleAlarmed(view);
            break;
        case STYLE_NORMAL:
            setStyleNormal(view);
            break;
        case STYLE_WARNING:
            setStyleWarning(view);
            break;
        case STYLE_SUMMARY_ALARM:
            setStyleSummaryAlarm(view);
            break;
        case STYLE_SUMMARY_WARNING:
            setStyleSummaryWarning(view);
            break;
        case STYLE_DISABLED:
            setStyleDisabled(view);
            break;
        default:
            break;
        }

    }

private void setStyleWarning(View view) {
    TextView titleView = (TextView) view.findViewById(android.R.id.title);
    titleView.setTextColor(Color.YELLOW);
            // add your FONT here
}

private void setStyleAlarmed(View view) {
    int alarmREDColor = view.getContext().getResources().getColor(R.color.alarmed_red);
    TextView titleView = (TextView) view.findViewById(android.R.id.title);
    titleView.setTextColor(alarmREDColor);
}

public void setStyle(int style) {
        switch (style) {
        case STYLE_ALARMED:
            this.style = STYLE_ALARMED;
            break;
        case STYLE_NORMAL:
            this.style = STYLE_NORMAL;
            break;
        default:
            this.style = STYLE_NORMAL;
        }
    }

public interface PreferenceStyle {

    public static final int STYLE_DISABLED = 0;
    public static final int STYLE_NORMAL = 10;
    public static final int STYLE_ALARMED = 20;
    public static final int STYLE_WARNING = 30;

    public static final int STYLE_SUMMARY_ALARM = 40;
    public static final int STYLE_SUMMARY_WARNING = 50;

    public void setStyle(int style);
    public Context getContext();
    public void setSummary(CharSequence summary);

}

public class YourActivity extends PreferenceActivity {

    private CustomPreference pref1;
    private CustomPreference pref2;
    private CustomPreference pref3;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);         
        addPreferencesFromResource(R.xml.prefence_xml);
        pref1 = findPreference();
                    pref1.setOnPreferenceClickListener(this);
                    pref1.setStyle(PreferenceStyle.STYLE_NORMAL);
               }
        }

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >


    <PreferenceCategory 
        android:title="category1">

        <com.project.main.preference.CustomPreference 
            android:key="key"
            android:title="title"
            />  

        <com.project.main.preference.CustomPreference 
            android:key="key"
            android:title="title"/>

    </PreferenceCategory>

</PreferenceScreen>
于 2012-11-02T10:40:37.523 に答える