gpt4 book ai didi

android - SetSpan 在 ScrollingActivity Title 中不起作用

转载 作者:太空狗 更新时间:2023-10-29 14:46:53 27 4
gpt4 key购买 nike

我设法通过以下代码在我的“正常” Activity 中更改了 AppBar 标题的字体:

setContentView(R.layout.activity_scrolling);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

SpannableString s = new SpannableString("MyTitle");
s.setSpan(new TypefaceSpan(this, "Britannic_Bold.ttf"), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
setTitle(s);

使用自定义类 TypefaceSpan:

public class TypefaceSpan extends MetricAffectingSpan {
/** An <code>LruCache</code> for previously loaded typefaces. */
private static LruCache<String, Typeface> sTypefaceCache =
new LruCache<String, Typeface>(12);

private Typeface mTypeface;

/**
* Load the {@link Typeface} and apply to a {@link Spannable}.
*/
public TypefaceSpan(Context context, String typefaceName) {
mTypeface = sTypefaceCache.get(typefaceName);

if (mTypeface == null) {
mTypeface = Typeface.createFromAsset(context.getApplicationContext()
.getAssets(), String.format("fonts/%s", typefaceName));

// Cache the loaded Typeface
sTypefaceCache.put(typefaceName, mTypeface);
}
}

@Override
public void updateMeasureState(TextPaint p) {
p.setTypeface(mTypeface);

// Note: This flag is required for proper typeface rendering
p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}

@Override
public void updateDrawState(TextPaint tp) {
tp.setTypeface(mTypeface);

// Note: This flag is required for proper typeface rendering
tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}

但是,这不适用于滚动 Activity (由 Android Studio 创建)

滚动 Activity 的标题不会更改其字体,但文本会按预期更改。

我已经尝试了很多,包括为工具栏标题创建样式,但它似乎不起作用。

有什么我可能监督的吗?

这是滚动 Activity 的 XML 部分:

<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

最佳答案

经过反复试验,我找到了解决方案!

只需获取 CollapsedToolbarLayout 并调用 setCollapsedTitleTypeface(customFont) 和 setExpandedTitleTypeface(customFont) !

工作起来很有魅力! :)

关于android - SetSpan 在 ScrollingActivity Title 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39090374/

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