【问题标题】:Toolbar Android L Custom TextView Font not applying工具栏 Android L 自定义 TextView 字体不适用
【发布时间】:2015-02-27 23:23:26
【问题描述】:

我对 android 中的新工具栏有疑问。

我有类似的布局:

<android.support.v7.widget.Toolbar
    android:id="@+id/tlb_wineoox_login"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:elevation="1dp"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary">

    <al.eng.utils.TextOratorStdMedium
        android:id="@+id/txt_home_acitivity_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:text="@string/app_name"
        android:layout_gravity="left"
        android:textColor="#3f3434"
        android:textSize="@dimen/tetembedhjet_sp" />
</android.support.v7.widget.Toolbar>

而自定义TextView的类是这样的:

public class TextOratorStdMedium extends TextView {

    public TextOratorStdMedium(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

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

    public TextOratorStdMedium(Context context) {
        super(context);
        init(context);
    }

    private void init(final Context context) {
        Typeface tf = Typeface.createFromAsset(context.getAssets(),"fonts/Orator-Std-Medium.ttf");
                setTypeface(tf);

    }
}

这样我的代码似乎并没有改变字体类型。但是,如果我在更改字体之前使用等待一秒钟的线程制作自定义文本视图,则它会起作用:

public class TextOratorStdMedium extends TextView {

    public TextOratorStdMedium(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

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

    public TextOratorStdMedium(Context context) {
        super(context);
        init(context);
    }

    private void init(final Context context) {
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Typeface tf = Typeface.createFromAsset(context.getAssets(),"fonts/Orator-Std-Medium.ttf");
                setTypeface(tf);
            }
        }, 1000);
    }
}

所以看起来工具栏在创建后以某种方式重写了自定义文本视图的字体。根本没有应用任何样式。这怎么可能?除了创建一个新线程并等待片刻之外,我们还有其他解决方案吗?

谢谢。

【问题讨论】:

  • 我尝试重现您的问题,一切正常...我认为问题可能出在不同的地方
  • @KonradKrakowiak 我的 ActionBarActivity 使用 PagerSlidingTabStrip 实现了一个 ViewPager。你认为这与它有关吗?

标签: android android-layout textview toolbar


【解决方案1】:

老实说,这看起来很奇怪。也许您可以尝试一些 View 生命周期方法。尝试覆盖 onMeasure()、onSizeChanged() 或 onAttachedToWindow(),但要注意递归调用。

【讨论】:

  • 我试过了,但似乎并没有改变问题。我的 nexus 5 有问题。
  • 我唯一想到的是您有自定义主题/样式,它会覆盖操作栏样式(或应用程序主题)。如果在工具栏之外,它可以工作吗?您是否尝试过棒棒糖的新构造函数? (这个有 4 个参数)
猜你喜欢
  • 1970-01-01
  • 2014-12-19
  • 2015-07-28
  • 2012-10-28
  • 1970-01-01
  • 2016-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多