【问题标题】:Set custom typeface to all TextView (except ActionBar)将自定义字体设置为所有 TextView(ActionBar 除外)
【发布时间】:2014-01-18 18:19:27
【问题描述】:

我想在我的 Android 应用程序中使用自定义字体。我使用以下方法将自定义字体设置为ActivityFragment 的所有TextView:

public static void setTypeFace(Typeface typeFace, ViewGroup parent){
    for (int i = 0; i < parent.getChildCount(); i++) {
        View v = parent.getChildAt(i);
        if (v instanceof ViewGroup) {
            setTypeFace(typeFace, (ViewGroup) v);
        } else if (v instanceof TextView) {
            TextView tv = (TextView) v;
            tv.setPaintFlags(tv.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
            tv.setTypeface(typeFace);
        }
    }
}

活动:

public class MyActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        ViewGroup vg = (ViewGroup)getWindow().getDecorView();
        ViewUtil.setTypeFace(tf, vg);
        ...
    }

效果很好。但不幸的是,ActionBar 的字体也发生了变化。我不想要那个..

如何调整此方法以排除 ActionBar 的 TextBox?

谢谢!

【问题讨论】:

  • 使用 android:textViewStyle 项定义自定义主题
  • 我已经更新了我的问题(添加 MyActivity 代码)。
  • 我重复一遍:谷歌关于主题和默认小部件样式
  • 如果你是一个 web 开发者,它就像一个 css 样式,但它们是默认应用的
  • 确定你可以做到:包含 ActionBar 子项的 ViewGroup 必须有一些固定的 id,使用 hierarchyviewer 检查它

标签: android android-ui android-typeface


【解决方案1】:

解决办法是确定两个ActionBar TextBox的ID名称:android:id/action_bar_titleandroid:id/action_bar_subtitle。您可以使用 hierarchyviewer SDK 工具来做到这一点(感谢@pskink!)。

public static void setTypeFace(Typeface typeFace, ViewGroup parent){
    for (int i = 0; i < parent.getChildCount(); i++) {
        View v = parent.getChildAt(i);
        if (v instanceof ViewGroup) {
            setTypeFace(typeFace, (ViewGroup) v);
        } else if (v instanceof TextView) {
            TextView tv = (TextView) v;
            String tvIdName = tv.getResources().getResourceName(tv.getId());
            if (!tvIdName.equals("android:id/action_bar_title") && !tvIdName.equals("android:id/action_bar_subtitle")) {
                if (DO_SUBPIXEL_RENDERING){
                    tv.setPaintFlags(tv.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
                }
                tv.setTypeface(typeFace);
            }
        }
    }
}

【讨论】:

  • 太棒了,顺便说一句,你可以比较整数 tv.getId() == R.id.action_bar_(sub)title
【解决方案2】:

您可以通过像这样覆盖 TextView 来创建自己的 TextView:

public class MyTextView extends TextView {

    public MyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setType(context);
    }

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

    public MyTextView(Context context) {
        super(context);
        setType(context);
    }

    private void setType(Context context){
        this.setTypeface(Typeface.createFromAsset(context.getAssets(),
                    "foo.ttf"));

        this.setShadowLayer(1.5f, 5, 5, getContext().getResources().getColor(R.color.black_shadow));
    }
}

并像这样使用它:

<com.your.project.package.MyTextView
        android:id="@+id/oppinfo_mtv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"  
        android:text="Player 1"
        />

【讨论】:

  • 是一个解决方案,但对我的使用来说不是很方便。
猜你喜欢
  • 2013-08-23
  • 1970-01-01
  • 2012-01-26
  • 1970-01-01
  • 2013-03-21
  • 2022-11-19
  • 1970-01-01
  • 2022-12-23
  • 2015-10-17
相关资源
最近更新 更多