久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

ActionBar 選項(xiàng)卡標(biāo)題中的自定義字體

Custom Typeface in ActionBar#39;s Tab Title(ActionBar 選項(xiàng)卡標(biāo)題中的自定義字體)
本文介紹了ActionBar 選項(xiàng)卡標(biāo)題中的自定義字體的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在嘗試在 ActionBar 選項(xiàng)卡的標(biāo)題上設(shè)置自定義字體.

I'm trying to set a custom Typeface on my ActionBar tabs' titles.

我看到更多的開發(fā)人員要求在 SO 上以適當(dāng)?shù)姆绞綀?zhí)行此操作(例如 如何自定義Action Bar標(biāo)簽的字體 & 我如何(如果可能)在選項(xiàng)卡文本的 ActionBar 中設(shè)置自定義字體,并在我的資產(chǎn)文件夾中設(shè)置字體?) 但沒有答案.

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) 第一個(gè)是啟發(fā)這個(gè) SO question 包括為每個(gè)標(biāo)簽添加自定義布局:

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);

這看起來不是一個(gè)很好的方法,因?yàn)槿绻x項(xiàng)卡 + 操作在橫向模式下不適合 ActionBar,則選項(xiàng)卡標(biāo)題會(huì)顯示在溢出列表(微調(diào)器/下拉菜單)中,但所選值顯示為空.當(dāng)您單擊此列表的項(xiàng)目時(shí),所有這些視圖都會(huì)消失.這尤其令人討厭,例如,當(dāng)用戶展開搜索操作視圖時(shí),會(huì)導(dǎo)致 android 將選項(xiàng)卡顯示為下拉菜單.

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 但字體不會(huì)更改為我的自定義字體.

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/..."中的字體設(shè)置 ActionBar 選項(xiàng)卡標(biāo)題的正確方法?任何幫助將不勝感激.

Does anyone know a proper way of styling the ActionBar tabs' titles with a Typeface from "/assets/fonts/..."? Any help would be greatly appreciated.

更多關(guān)于第二種方法.我使用的 TypefaceSpan 類實(shí)際上是 android.text.style.TypefaceSpan 由用戶@twaddington 在這里展示:如何在ActionBar Title中設(shè)置自定義字體?

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:

在上一個(gè)鏈接中,有一條評(píng)論指出:如果在底層 TextView 上將 textAllCaps 屬性設(shè)置為 true(例如通過主題),那么自定義字體將不會(huì)出現(xiàn).這對(duì)我來說是個(gè)問題當(dāng)我將此技術(shù)應(yīng)用于操作欄選項(xiàng)卡項(xiàng)時(shí)".

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".

我已經(jīng)改變了我的風(fēng)格,將 textAllCaps 設(shè)置為 false,現(xiàn)在第二種方法似乎有效.我會(huì)測(cè)試一下并發(fā)布結(jié)果.

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.

結(jié)論:

之前編輯中的解決方案似乎有效.

The solution in the previous edit seems to work.

將@CommonsWare 的答案標(biāo)記為正確的相關(guān)性.

Marking @CommonsWare's answer as correct for its relevancy.

@PeteH 的 PS

我在 6 個(gè)月前問過這個(gè)問題,所以我不記得所有細(xì)節(jié)了.我相信對(duì)于這個(gè)應(yīng)用程序,我最終采用了不同的導(dǎo)航方法.我現(xiàn)在可以在應(yīng)用程序中找到的所有內(nèi)容(關(guān)于滑動(dòng)...)是一個(gè) Activity,其布局包含一個(gè)帶有 PagerTabStripViewPager,我樣式如下:

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.

我能找到的唯一相關(guān)代碼是這個(gè),我在其中設(shè)置了一個(gè) 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 選項(xiàng)卡文本的樣式如下:

...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 僅適用于三種內(nèi)置字體.

TypefaceSpan will only work with the three built-in typefaces.

話雖如此,派生 TypefaceSpan 以使用從本地文件路徑加載 Typeface 的地方應(yīng)該不難.與其將構(gòu)造函數(shù)中的 String 視為面部??家族名稱,不如將其視為本地文件路徑,調(diào)整 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.

從資產(chǎn)中獲取 Typeface 的問題是 TypefaceSpan 需要訪問 AssetManager,而且它不容易訪問在放入 Parcel 并從其重建后變?yōu)橐粋€(gè).

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.

我沒有使用您的第一種技術(shù),但您遇到問題我并不感到驚訝.

I have not used your first technique, but I am not surprised that you are having problems.

您也可以考慮完全放棄操作欄選項(xiàng)卡并切換到帶有選項(xiàng)卡式指示器的 ViewPager,因?yàn)槟梢愿p松地進(jìn)行樣式設(shè)置,例如 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.

這篇關(guān)于ActionBar 選項(xiàng)卡標(biāo)題中的自定義字體的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動(dòng)生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫(kù))
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對(duì)象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 午夜在线影院 | www天天操| 午夜精品一区二区三区在线观看 | 亚洲视频在线观看 | 国产日韩一区二区 | 国产视频一区二区在线观看 | 亚洲一级av毛片 | 欧美白人做受xxxx视频 | 精品视频一区二区三区在线观看 | 在线资源视频 | 在线免费亚洲视频 | 国产精品久久久久久 | 一区二区三区四区av | 中文字幕二区 | 老司机精品福利视频 | 国产欧美日韩在线一区 | 91精品国产91久久久久久三级 | 国产精品乱码一区二区三区 | 日韩精品一区二区三区中文在线 | 久久久久久亚洲精品 | 精品国产一区二区三区久久久久久 | 久久久精品一区二区三区 | 精品粉嫩aⅴ一区二区三区四区 | 神马福利 | 国产高清在线观看 | 中文字幕在线一区二区三区 | 欧美一级欧美一级在线播放 | 91免费在线看 | 91在线观看网址 | 亚洲人成网亚洲欧洲无码 | 日韩视频国产 | 精品视频一区二区三区在线观看 | 在线视频国产一区 | 天堂一区二区三区四区 | 一区在线观看 | 日本中文在线视频 | 日韩精品一区二区三区中文在线 | 日韩电影免费在线观看中文字幕 | 中文字幕av一区 | 久久综合影院 | 精品久久久久久久久久久久久久 |