在动作条的标签名称自定义字样自定义、字样、名称、动作

2023-09-12 02:34:27 作者:尴尬症·

我想在我的动作条标签'标题设置自定义字体。

我已经看到了更多的开发要求一个适当的方式做到这一点的SO(如的如何自定义的操作栏选项卡上的字体和放大器; How (如果可能的话),我可以在设置选项卡上的文字动作条自定义字体,在我的资产文件夹中的字体?),但没有答案。

到目前为止,我已经按照两种方法:

1),第一个是灵感的本SO问题,由膨胀的自定义布局为每个标签的:

  LayoutInflater吹气= LayoutInflater.from(本);
查看customView = inflater.inflate(R.layout.tab_title,NULL); //为分页标题自定义布局,基本上包含一个TextView ...

TextView的titleTV =(TextView中)customView.findViewById(R.id.action_custom_title);
        titleTV.setText(mSectionsPagerAdapter.getPageTitle(ⅰ));
        titleTV.setGravity(Gravity.CENTER_VERTICAL);
        titleTV.setTypeface(((MyApp的)getApplicationContext())getCustomTypeface());

// ...在这里,我还可以加我想任何其他造型...

actionBar.getTabAt(ⅰ).setCustomView(customView);
 

这看起来并不像一个很好的方法,因为如果标签+操作不适合的动作条在横向模式下,该选项卡标题显示在溢出列表(旋转/下拉),但所选择的价值显示为空。当你点击这个名单的项目,那么所有这些视图中消失。这是特别讨厌的,例如当用户展开搜索行动来看这将导致Android的显示选项卡,在下拉。

2)我已经试过另一种方法为presented的此处这涉及到使用 SpannableString 但字体不会更改为我的自定义之一。

  SpannableString S =新SpannableString(mSectionsPagerAdapter.getPageTitle(一));
s.setSpan(新TypefaceSpan(此,FontName.ttf),0,s.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
actionBar.addTab(actionBar.newTab()的setText(S).setTabListener(本)。);
 

类TypefaceSpan可以看出这里。

所以...

有谁知道造型的动作条标签的标题从/资源/字体/ ...字样的正确方法?任何帮助将大大AP preciated。

编辑:

更多关于第二种方法。 我使用的TypefaceSpan类实际上是 android.text.style.TypefaceSpan 通过这里用户@twaddington psented $ P $:How设置自定义字体的动作条标题?

编辑2:

怎么在手机里把百度的电脑版切换成手机版

在previous链接,评论说:如果textAllCaps属性设置为true底层的TextView上(例如,通过一个主题),然后将自定义的字体将不会出现这是一个问题对我来说,当我应用这个技术动作栏选项卡项目的。

我已经改变了我的风格,使textAllCaps设置为false,现在第二种方法似乎工作。我会测试了一下,后的结果。

结论:

在previous编辑的解决方案似乎工作。

标记@ CommonsWare的答案是正确的相关性。

PS-编辑@PeteH:

我问这个4个月前,所以我不记得所有的细节。我相信,这个程序我结束了以下不同的方法进行导航。所有我现在可以在应用程序(关于刷卡...)是一个活动,其布局包含 ViewPager 与一个 PagerTabStrip ,这是我的风格是这样的:

  //样式的标签条:
字体TF =((MyApplication的)getApplication())getTabStripTypeface()。 //用它来保持字体(Singleton模式)的一个实例,避免纪念品。泄漏
PagerTabStrip条=(PagerTabStrip)findViewById(R.id.pager_title_strip);
strip.setTabIndicatorColor(getResources()的getColor(R.color.myColor));
strip.setDrawFullUnderline(真正的);
的for(int i = 0; I< strip.getChildCount(); ++ I){
    查看nextChild = strip.getChildAt(我);
    如果(nextChild的instanceof的TextView){
        TextView的textViewToConvert =(TextView中)nextChild;
                    textViewToConvert.setAllCaps(假);
        textViewToConvert.setTypeface(TF);
    }
}
 

这是不一样的psented在这个问题的问题$ P $,虽然。

唯一相关的code我能找到的就是这个,在这里我设置了 SpannableString

  //对于每个应用程序的章节中,添加一个标签,操作栏。
的for(int i = 0; I< mSectionsPagerAdapter.getCount();我++){
    SpannableString S =新SpannableString(mSectionsPagerAdapter.getPageTitle(一));
    s.setSpan(新TypefaceSpan(此,FontName.ttf),0,s.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    actionBar.addTab(actionBar.newTab()的setText(S).setTabListener(本)。);
}
 

......而在我的styles.xml,我有风格类似这样的动作条的标签文字:

 <! - 行动起来吧tabtext风格 - >
<样式名称=ActionBarTabText.MyApplication父=@安卓风格/ Widget.Holo.ActionBar.TabText>
    <项目名称=机器人:textAppearance> @android:款式/ TextAppearance.Holo.Medium< /项目>
    <项目名称=机器人:文字颜色>安卓?ATTR / textColorPrimaryInverse< /项目>
    <项目名称=机器人:TEXTSIZE> 15sp< /项目>
    <项目名称=机器人:TEXTSTYLE>大胆< /项目>
    <项目名称=机器人:textAllCaps>假< /项目>
    <项目名称=机器人:ellipsize>字幕< /项目>
    <项目名称=机器人:MAXLINES> 1< /项目>
< /风格>
 

解决方案

TypefaceSpan 将只与三个工作内置字体。

话虽这么说,分叉 TypefaceSpan 使用一个其中字样从本地文件加载路径不应该是那么难。而不是把字符串在构造函数中作为一个面对家人的名字,你只会把它作为一个本地文件路径,调整适用()来从那里加载它。 字样本身并不 Parcelable ,所以你需要的路径工作。

与得到的问题字样从资产的 TypefaceSpan 将需要访问 AssetManager ,它会不容易有被放进后访问一个和重建了包裹

我没有使用你的第一个技巧,但我并不感到惊讶,您有问题。

您也可以考虑下探动作栏选​​项卡完全并切换到 ViewPager 带标签的指标,因为你可能有更容易的造型,比方说,杰克沃顿商学院的 TabPageIndicator

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

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

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

Class TypefaceSpan can be seen here.

So...

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

EDIT:

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?

EDIT 2:

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

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.

Conclusion:

The solution in the previous edit seems to work.

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

PS-EDIT for @PeteH:

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.

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

...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 will only work with the three built-in typefaces.

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.

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.

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.

 
精彩推荐
图片推荐