Java ActionBar'中的自定义字体;s标签标题
我正在尝试在我的Java ActionBar'中的自定义字体;s标签标题,java,android,android-layout,tabs,android-actionbar,Java,Android,Android Layout,Tabs,Android Actionbar,我正在尝试在我的ActionBar选项卡标题上设置自定义字体 我看到更多的开发人员要求在SO上使用适当的方法(例如&),但没有答案 到目前为止,我采用了两种方法: 1)第一个选项卡的灵感来源于此,包括为每个选项卡扩展自定义布局: LayoutInflater inflater = LayoutInflater.from(this); View customView = inflater.inflate(R.layout.tab_title, null); // a custom layout f
ActionBar
选项卡标题上设置自定义字体
我看到更多的开发人员要求在SO上使用适当的方法(例如&),但没有答案
到目前为止,我采用了两种方法:
1)第一个选项卡的灵感来源于此,包括为每个选项卡扩展自定义布局:
LayoutInflater inflater = LayoutInflater.from(this);
View customView = inflater.inflate(R.layout.tab_title, null); // a custom layout for the tab title, basically contains a textview...
TextView titleTV = (TextView) customView.findViewById(R.id.action_custom_title);
titleTV.setText(mSectionsPagerAdapter.getPageTitle(i));
titleTV.setGravity(Gravity.CENTER_VERTICAL);
titleTV.setTypeface(((MyApp) getApplicationContext()).getCustomTypeface());
// ...Here I could also add any other styling I wanted to...
actionBar.getTabAt(i).setCustomView(customView);
这看起来不是一个很好的方法,因为如果选项卡+操作不适合横向模式下的ActionBar,则选项卡标题将显示在溢出列表(微调器/下拉列表)中,但所选值显示为空。当您单击此列表项时,所有这些视图都将消失。例如,当用户展开搜索操作视图,导致android将选项卡显示为下拉列表时,这尤其令人讨厌
2)我尝试了前面介绍的另一种方法,即使用SpannableString
,但字体不会更改为我的自定义字体
SpannableString s = new SpannableString(mSectionsPagerAdapter.getPageTitle(i));
s.setSpan(new TypefaceSpan(this, "FontName.ttf"), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
actionBar.addTab(actionBar.newTab().setText(s).setTabListener(this));
可以看到类的字体跨度
所以…
有人知道用“/assets/fonts/…”的字体设计ActionBar
tabs”标题的正确方法吗?任何帮助都将不胜感激
编辑:
LayoutInflater inflater = LayoutInflater.from(this);
View customView = inflater.inflate(R.layout.tab_title, null); // a custom layout for the tab title, basically contains a textview...
TextView titleTV = (TextView) customView.findViewById(R.id.action_custom_title);
titleTV.setText(mSectionsPagerAdapter.getPageTitle(i));
titleTV.setGravity(Gravity.CENTER_VERTICAL);
titleTV.setTypeface(((MyApp) getApplicationContext()).getCustomTypeface());
// ...Here I could also add any other styling I wanted to...
actionBar.getTabAt(i).setCustomView(customView);
更多关于第二种方法。
我使用的TypefaceSpan类实际上是用户@twaddington在这里展示的一个分支:
编辑2:
LayoutInflater inflater = LayoutInflater.from(this);
View customView = inflater.inflate(R.layout.tab_title, null); // a custom layout for the tab title, basically contains a textview...
TextView titleTV = (TextView) customView.findViewById(R.id.action_custom_title);
titleTV.setText(mSectionsPagerAdapter.getPageTitle(i));
titleTV.setGravity(Gravity.CENTER_VERTICAL);
titleTV.setTypeface(((MyApp) getApplicationContext()).getCustomTypeface());
// ...Here I could also add any other styling I wanted to...
actionBar.getTabAt(i).setCustomView(customView);
在上一个链接中,有一条评论指出:“如果在基础TextView(例如通过主题)上将textAllCaps属性设置为true,则自定义字体将不会出现。这是我在将此技术应用于操作栏选项卡项时遇到的问题。”
我已经改变了我的风格,所以textAllCaps被设置为false,现在第二种方法似乎起作用了。我会测试一下,然后公布结果
结论:
LayoutInflater inflater = LayoutInflater.from(this);
View customView = inflater.inflate(R.layout.tab_title, null); // a custom layout for the tab title, basically contains a textview...
TextView titleTV = (TextView) customView.findViewById(R.id.action_custom_title);
titleTV.setText(mSectionsPagerAdapter.getPageTitle(i));
titleTV.setGravity(Gravity.CENTER_VERTICAL);
titleTV.setTypeface(((MyApp) getApplicationContext()).getCustomTypeface());
// ...Here I could also add any other styling I wanted to...
actionBar.getTabAt(i).setCustomView(customView);
上一次编辑中的解决方案似乎有效
将@commonware的答案标记为其相关性正确
PS-EDIT for@PeteH:
LayoutInflater inflater = LayoutInflater.from(this);
View customView = inflater.inflate(R.layout.tab_title, null); // a custom layout for the tab title, basically contains a textview...
TextView titleTV = (TextView) customView.findViewById(R.id.action_custom_title);
titleTV.setText(mSectionsPagerAdapter.getPageTitle(i));
titleTV.setGravity(Gravity.CENTER_VERTICAL);
titleTV.setTypeface(((MyApp) getApplicationContext()).getCustomTypeface());
// ...Here I could also add any other styling I wanted to...
actionBar.getTabAt(i).setCustomView(customView);
我6个月前问过这个问题,所以我不记得所有的细节。我相信,对于这个应用程序,我最终采用了一种不同的导航方法。我现在在应用程序(关于刷卡…)中只能找到一个活动
,它的布局包含一个带有PagerTabStrip
的ViewPager
,我的样式如下:
// Style the Tab Strip:
Typeface tf = ((MyApplication) getApplication()).getTabStripTypeface(); // Used this to keep a single instance of the typeface (singleton pattern) and avoid mem. leaks
PagerTabStrip strip = (PagerTabStrip) findViewById(R.id.pager_title_strip);
strip.setTabIndicatorColor(getResources().getColor(R.color.myColor));
strip.setDrawFullUnderline(true);
for (int i = 0; i < strip.getChildCount(); ++i) {
View nextChild = strip.getChildAt(i);
if (nextChild instanceof TextView) {
TextView textViewToConvert = (TextView) nextChild;
textViewToConvert.setAllCaps(false);
textViewToConvert.setTypeface(tf);
}
}
<!-- action bar tabtext style -->
<style name="ActionBarTabText.MyApplication" parent="@android:style/Widget.Holo.ActionBar.TabText">
<item name="android:textAppearance">@android:style/TextAppearance.Holo.Medium</item>
<item name="android:textColor">?android:attr/textColorPrimaryInverse</item>
<item name="android:textSize">15sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textAllCaps">false</item>
<item name="android:ellipsize">marquee</item>
<item name="android:maxLines">1</item>
</style>
…而在my styles.xml中,Actionbar选项卡文本的样式如下:
// Style the Tab Strip:
Typeface tf = ((MyApplication) getApplication()).getTabStripTypeface(); // Used this to keep a single instance of the typeface (singleton pattern) and avoid mem. leaks
PagerTabStrip strip = (PagerTabStrip) findViewById(R.id.pager_title_strip);
strip.setTabIndicatorColor(getResources().getColor(R.color.myColor));
strip.setDrawFullUnderline(true);
for (int i = 0; i < strip.getChildCount(); ++i) {
View nextChild = strip.getChildAt(i);
if (nextChild instanceof TextView) {
TextView textViewToConvert = (TextView) nextChild;
textViewToConvert.setAllCaps(false);
textViewToConvert.setTypeface(tf);
}
}
<!-- action bar tabtext style -->
<style name="ActionBarTabText.MyApplication" parent="@android:style/Widget.Holo.ActionBar.TabText">
<item name="android:textAppearance">@android:style/TextAppearance.Holo.Medium</item>
<item name="android:textColor">?android:attr/textColorPrimaryInverse</item>
<item name="android:textSize">15sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textAllCaps">false</item>
<item name="android:ellipsize">marquee</item>
<item name="android:maxLines">1</item>
</style>
@android:style/TextAppearance.Holo.Medium
?android:attr/textColorPrimaryInverse
15便士
大胆的
假的
帐篷
1.
字体span
仅适用于三种内置字体
这就是说,使用从本地文件路径加载字体的方法应该不会那么困难。与其将构造函数中的字符串
视为面族名称,不如将其视为本地文件路径,调整apply()
以从那里加载<代码>字体
本身不可包裹
,因此需要使用路径
从资产中获取字体的问题在于字体span
将需要访问资产管理器
,并且在放入地块
并从中重建后,它将不容易访问
我没有使用你的第一种技术,但我并不奇怪你有问题
您还可以考虑完全删除Action Bar标签,并使用带标签的指示器切换到<代码> ViewPager < /C>,因为您可能有更简单的时间样式,例如,Jake Wharton的“代码> TabPoGeigule”
< P>。
[
然后创建一个布局,并使用以下代码将其命名为tab_title:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/action_custom_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Custom title"
android:textColor="#fff"
android:textSize="18sp"
android:paddingTop="5dp" />
致:
(无需说明您必须将font/titr.TTF更改为资产目录和文件名)
Joint it!!我已经在使用Typeface span(问题编辑)的自定义实现,它从资源加载字体(基于此处显示的用户@twaddington的一些代码:)@user1987392:就我个人而言,我觉得他的实现有点令人不安,因为我预计在操作栏标题出现在包裹
中的情况下会出现问题。也许这通常不会发生。无论如何,如果您正在使用他的代码,请从字体名称中删除.ttf
。他的实现会附加.otf
并假设您的字体位于字体/
的资产/
中。如果您确实在使用.ttf
文件和/或您的字体位于资产/
的其他位置,您需要进一步调整他的代码。我修改了他的代码,以便它考虑到提供给承包商的字体名称中的扩展名调试时,我可以看到Typeface对象已创建(否则程序将崩溃)。我也不太喜欢这种方法,我开始觉得有点倾向于使用建议的TabPageIndicator或尝试一下ActionBarSherlock,尽管我更喜欢使用默认方法。@Commonware底层的MetricAffectingSpan
不是一个可打包的对象。实现非常简单。它只是加载一个字体,将其弹出到内存缓存中,并更新用于绘制文本的TextPaint
。这都是正常的框架API代码。如果您对如何改进有任何建议,请发送电子邮件给我。我们已经在生产中使用此代码将近一年了,没有任何问题。.ttf
不过反馈是公平的,我已经更新了原始答案。@user1987392:请您提供实际对您有效的方法的代码,好吗?我以与您完全相同的顺序独立地解决了这个问题,但仍然无法找到有效的解决方案。(我承认,看到自己不是唯一一个做不到的人,我感觉好多了