問題描述
我正在嘗試在 ActionBar
選項卡的標題上設置自定義字體.
I'm trying to set a custom Typeface on my ActionBar
tabs' titles.
我看到更多的開發人員要求在 SO 上以適當的方式執行此操作(例如 如何自定義Action Bar標簽的字體 & 我如何(如果可能)在選項卡文本的 ActionBar 中設置自定義字體,并在我的資產文件夾中設置字體?) 但沒有答案.
I have seen more developers asking for a proper way to do this on SO (e.g. How to customize the font of Action Bar tabs & How (if possible) could I set a custom font in a ActionBar on tab text with a font in my assets folder?) but no answers.
到目前為止,我采用了兩種方法:
So far, I've followed two approaches:
1) 第一個是啟發這個 SO question 包括為每個標簽添加自定義布局:
1) The first one was inspired by this SO question and consists of inflating a custom layout for each tab:
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 將選項卡顯示為下拉菜單.
This doesn't look like a very good approach because if the tabs + actions don't fit to the ActionBar in landscape mode, the tab titles are displayed in an overflow list (Spinner/Drop-down) but the selected value shows up as empty. And when you click on this list's item, then all of these views disappear. This is particularly annoying when, for example the user expands a search action view which causes android to show the tab as a Drop-down.
2) 我嘗試了另一種方法,如 here 涉及使用 SpannableString
但字體不會更改為我的自定義字體.
2) I've tried another approach as presented here which involves using a SpannableString
but the font doesn't change to my custom one.
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));
可以在這里查看類TypefaceSpan.
Class TypefaceSpan can be seen here.
那么...
有誰知道使用/assets/fonts/..."中的字體設置 ActionBar
選項卡標題的正確方法?任何幫助將不勝感激.
Does anyone know a proper way of styling the ActionBar
tabs' titles with a Typeface from "/assets/fonts/..."? Any help would be greatly appreciated.
更多關于第二種方法.我使用的 TypefaceSpan 類實際上是 android.text.style.TypefaceSpan 由用戶@twaddington 在這里展示:如何在ActionBar Title中設置自定義字體?
More on the second approach. The TypefaceSpan class I'm using is actually a fork of android.text.style.TypefaceSpan as presented by user @twaddington here: How to Set a Custom Font in the ActionBar Title?
編輯 2:
在上一個鏈接中,有一條評論指出:如果在底層 TextView 上將 textAllCaps 屬性設置為 true(例如通過主題),那么自定義字體將不會出現.這對我來說是個問題當我將此技術應用于操作欄選項卡項時".
In the previous link, a comment stated: "if the textAllCaps attribute is set to true on the underlying TextView (e.g. via a theme), then the custom font won't appear. This was an issue for me when I applied this technique to the action bar tab items".
我已經改變了我的風格,將 textAllCaps 設置為 false,現在第二種方法似乎有效.我會測試一下并發布結果.
I have changed my style so that textAllCaps was set to false and now the second method seems to work. I'll test it a bit and post the results.
結論:
之前編輯中的解決方案似乎有效.
The solution in the previous edit seems to work.
將@CommonsWare 的答案標記為正確的相關性.
Marking @CommonsWare's answer as correct for its relevancy.
@PeteH 的 PS
我在 6 個月前問過這個問題,所以我不記得所有細節了.我相信對于這個應用程序,我最終采用了不同的導航方法.我現在可以在應用程序中找到的所有內容(關于滑動...)是一個 Activity
,其布局包含一個帶有 PagerTabStrip
的 ViewPager
,我樣式如下:
I asked this 6 months ago so I don't remember all the details. I believe that for this app I ended up following a different approach for navigation. All I can find now in the app (regarding swiping...) is an Activity
whose layout contains a ViewPager
with a PagerTabStrip
, which I styled like this:
// 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);
}
}
不過,這與此問題中提出的問題不同.
This is not the same as the issue presented in this question, though.
我能找到的唯一相關代碼是這個,我在其中設置了一個 SpannableString
:
The only related code I can find is this, where I set a 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));
}
...而在我的styles.xml 中,我的Actionbar 選項卡文本的樣式如下:
...whereas in my styles.xml, I had the style for the Actionbar's Tab Text like this:
<!-- 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>
推薦答案
TypefaceSpan
僅適用于三種內置字體.
TypefaceSpan
will only work with the three built-in typefaces.
話雖如此,派生 TypefaceSpan
以使用從本地文件路徑加載 Typeface
的地方應該不難.與其將構造函數中的 String
視為面部??家族名稱,不如將其視為本地文件路徑,調整 apply()
以從那里加載它.Typeface
本身不是 Parcelable
,因此您需要使用路徑.
That being said, forking TypefaceSpan
to use one where the Typeface
is loaded from a local file path should not be that hard. Rather than treating the String
in the constructor as being a face family name, you would treat it as an local file path, adjusting apply()
to load it from there. Typeface
itself is not Parcelable
, so you would need to work with the path.
從資產中獲取 Typeface
的問題是 TypefaceSpan
需要訪問 AssetManager
,而且它不容易訪問在放入 Parcel
并從其重建后變為一個.
The problem with getting the Typeface
from assets is that the TypefaceSpan
would need access to an AssetManager
, and it would not readily have access to one after being put into and rebuilt from a Parcel
.
我沒有使用您的第一種技術,但您遇到問題我并不感到驚訝.
I have not used your first technique, but I am not surprised that you are having problems.
您也可以考慮完全放棄操作欄選項卡并切換到帶有選項卡式指示器的 ViewPager
,因為您可以更輕松地進行樣式設置,例如 Jake Wharton 的 TabPageIndicator
.
You might also consider dropping action bar tabs entirely and switching to a ViewPager
with a tabbed indicator, as you may have an easier time styling, say, Jake Wharton's TabPageIndicator
.
這篇關于ActionBar 選項卡標題中的自定義字體的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!