gpt4 book ai didi

android - 在 PreferenceScreen 中为文本设置自定义字体

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:58:56 25 4
gpt4 key购买 nike

我的 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 中的文本?

提前致谢!

最佳答案

更改 PreferenceScreen 中所有 TextViews 字体的最简单方法是:

public class WallpaperSettings extends PreferenceActivity {

private View tryInflate(String name, Context context, AttributeSet attrs) {
LayoutInflater li = LayoutInflater.from(context);
View v = null;
try {
v = li.createView(name, null, attrs);
} catch (Exception e) {
try {
v = li.createView("android.widget." + name, null, attrs);
} catch (Exception e1) {
}
}
return v;
}

protected void onCreate(Bundle savedInstanceState) {
//CHANGE TYPEFACE FOR ALL TEXTVIEWS
final Typeface font = Typeface.createFromAsset(getAssets(), "fonts/cabal.ttf");
getLayoutInflater().setFactory(new Factory() {
@Override
public View onCreateView(String name, Context context,
AttributeSet attrs) {
View v = tryInflate(name, context, attrs);
if (v instanceof TextView) {
((TextView) v).setTypeface(font);
}
return v;
}
});

super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}

就是这样!

关于android - 在 PreferenceScreen 中为文本设置自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13193405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com