1

//我正在设置具有各种内容的不同TextView作为文本我正在做的是为所有textview设置字体。自定义字体也显示正常。

((TextView) findViewById    (R.id.tutor_ps_tv_home))        .setTypeface(mFace);
((TextView) findViewById    (R.id.tutor_ps_tv_plus))        .setTypeface(mFace);
((TextView) findViewById    (R.id.tutor_ps_tv_settings))    .setTypeface(mFace);
((TextView) findViewById    (R.id.tutor_ps_tv_undo))        .setTypeface(mFace);
((TextView) findViewById    (R.id.tutor_ps_tv_cam))         .setTypeface(mFace);
((TextView) findViewById    (R.id.tutor_ps_tv_share))       .setTypeface(mFace);

//是否可以为所有文本视图仅设置一个 id 来更改我的字体,如下所示。

 android:id="@+id/all_textview"
 android:id="@id/all_textview"
 android:id="@id/all_textview"
 android:id="@id/all_textview"

//我将在我的活动中设置为

 ((TextView) findViewById   (R.id.all_textview))        .setTypeface(mFace);

// 有没有人知道更好的解决方案。

注意:我只想更改字体而不是其他任何东西/与 textview 一起使用。

4

4 回答 4

1

是否可以为所有文本视图仅设置一个 id 来更改我的字体,如下所示。

findViewById(),仍然会返回一个TextView——它只是你给的同名者中的一个随机的。

在 StackOverflow 和其他地方有一些例子可以遍历内容视图的子项,找到符合您条件的那些(例如, are TextViews)并更改它们的Typeface.

于 2012-07-11T11:29:03.663 回答
1

看来这篇文章会对你有所帮助

Android:想要为整个应用程序而不是运行时设置自定义字体

该帖子的答案描述了一种方法。您可以将所有文本视图作为可变参数传递给函数并在那里更改字体

最后有 11 票的答案(倒数第二个)。答案描述了使用具有此字体集的自定义扩展文本视图。我个人觉得这是更好的方法。

这是另一个帖子

是否可以为整个应用程序设置自定义字体?

这里的回复帖子Tom描述了我们如何为活动中的所有视图设置相同的字体

于 2012-07-11T11:32:53.083 回答
0

我还认为您应该将您的 typeFace 设置为一种样式并将其应用于您的应用程序或您的活动,您还可以从 textView 继承您的样式并将所有 textviews 默认设置为定义的样式。

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont" 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>
</resources>

问候。:)

于 2012-07-11T11:33:49.467 回答
-1

在 res/values/styles.xml 中定义一个样式并将其应用到您的 Activity。有关详细信息,请参阅:http: //developer.android.com/guide/topics/ui/themes.html

编辑:

我的错。如果 CommonsWare 说我错了,那我就错了。:-)

于 2012-07-11T11:26:56.633 回答