use*_*392 5 java tabs android android-layout android-actionbar
我正在尝试在ActionBar标签的标题上设置自定义字体.
我看到更多的开发者,要求以适当的方法等做这个的(例如,如何自定义操作栏标签的字体和如何(如果可能的话),我可以设置选项卡上的文字动作条自定义字体,在我的资产字体文件夹?)但没有答案.
到目前为止,我遵循了两种方法:
1)第一个受到这个SO问题的启发,包括为每个标签膨胀自定义布局:
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);
Run Code Online (Sandbox Code Playgroud)
这看起来不是一个非常好的方法,因为如果选项卡+操作不适合横向模式下的ActionBar,则选项卡标题将显示在溢出列表(Spinner/Drop-down)中,但所选值显示为空.当您单击此列表的项目时,所有这些视图都会消失.当例如用户扩展搜索动作视图时导致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));
Run Code Online (Sandbox Code Playgroud)
Class TypefaceSpan可以在这里看到.
所以...
有没有人知道ActionBar用"/ assets/fonts/..."字体设置标签标题的正确方法?任何帮助将不胜感激.
编辑:
更多关于第二种方法.我正在使用的TypefaceSpan类实际上是用户@twaddington在这里提供的android.text.style.TypefaceSpan的一个分支:如何在ActionBar标题中设置自定义字体?
编辑2:
在上一个链接中,评论声明:"如果textAllCaps属性在底层TextView上设置为true(例如通过主题),那么自定义字体将不会出现.当我将此技术应用于此时,这对我来说是个问题.操作栏标签项".
我已经改变了我的样式,因此textAllCaps设置为false,现在第二种方法似乎有效.我会测试一下并发布结果.
结论:
上一个编辑中的解决方案似乎有效.
将@ CommonsWare的答案标记为其相关性是正确的.
PS编辑@PeteH:
我6个月前问过这个,所以我不记得所有的细节.我相信对于这个应用程序,我最终采用了不同的导航方法.我现在可以在应用程序中找到的所有内容(关于刷卡...)是一个Activity布局包含a ViewPager的a PagerTabStrip,我的样式如下:
// 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);
}
}
Run Code Online (Sandbox Code Playgroud)
但这与此问题中提出的问题不同.
我能找到的唯一相关代码就是这个,我设置了一个SpannableString:
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
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));
}
Run Code Online (Sandbox Code Playgroud)
...而在我的styles.xml中,我有Actionbar的Tab Text的样式,如下所示:
<!-- 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>
Run Code Online (Sandbox Code Playgroud)
TypefaceSpan仅适用于三种内置字体。
话虽这么说,分叉TypefaceSpan以使用Typeface从本地文件路径加载的文件应该不那么困难。您不应将String构造函数中的 视为面部家族名称,而是将其视为本地文件路径,并进行调整apply()以从那里加载它。Typeface本身不是Parcelable,所以你需要使用路径。
从资产获取 的问题Typeface在于,TypefaceSpan需要访问 .a AssetManager,并且在放入 .a 并从 .a 重建后,它不会轻易访问 .a Parcel。
我没有使用过你的第一种技术,但我对你遇到问题并不感到惊讶。
您还可以考虑完全删除操作栏选项卡并切换到ViewPager带有选项卡式指示器的选项卡,因为您可能会更轻松地设计样式,例如 Jake Wharton 的TabPageIndicator.
| 归档时间: |
|
| 查看次数: |
6313 次 |
| 最近记录: |