如何在操作栏中制作自定义字体立即显示

时间:2014-07-10 07:52:53

标签: java android xml android-layout android-actionbar

为了更改操作栏内的字体,我实现了一个自定义的TypefaceSpan类,并在我的onCreate中使用了SpannableString。

这就是我所做的: How to Set a Custom Font in the ActionBar Title?

在评论用户中,艺术品说,

  

在启动应用程序时,默认标题样式可见   大约1秒钟后出现自定义样式。

那个回答问题的人说

  

“那是因为你的活动的主题正在应用于   在您的代码启动之前的DecorView。你可以解决这个问题   隐藏主题中的操作栏,然后将其显示为您的活动   被创造。“

我不确定如何实施以下内容。我也不确定能否在我的自定义操作栏中完成。

这就是我设置动作栏的方式:

<?xml version="1.0" encoding="utf-8"?>



<resources>

    <style name="Theme.Customaction" parent="@style/android:Theme.Holo">
        <item name="actionBarItemBackground">@drawable/selectable_background_customaction</item>
        <item name="popupMenuStyle">@style/PopupMenu.Customaction</item>
        <item name="dropDownListViewStyle">@style/DropDownListView.Customaction</item>
        <item name="actionBarTabStyle">@style/ActionBarTabStyle.Customaction</item>
        <item name="actionDropDownStyle">@style/DropDownNav.Customaction</item>
        <item name="actionBarStyle">@style/ActionBar.Transparent.Customaction</item>
        <item name="actionModeBackground">@drawable/cab_background_top_customaction</item>
        <item name="actionModeSplitBackground">@drawable/cab_background_bottom_customaction</item>
        <item name="actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Customaction</item>


    </style>

    <style name="ActionBar.Solid.Customaction" parent="@style/Widget.AppCompat.ActionBar.Solid">
        <item name="background">@drawable/ab_solid_customaction</item>
        <item name="backgroundStacked">@drawable/ab_stacked_solid_customaction</item>
        <item name="backgroundSplit">@drawable/ab_bottom_solid_customaction</item>
        <item name="progressBarStyle">@style/ProgressBar.Customaction</item>
    </style>

    <style name="ActionBar.Transparent.Customaction" parent="@style/Widget.AppCompat.ActionBar">
        <item name="background">@drawable/ab_transparent_customaction</item>
        <item name="progressBarStyle">@style/ProgressBar.Customaction</item>
    </style>

    <style name="PopupMenu.Customaction" parent="@style/Widget.AppCompat.PopupMenu">
        <item name="android:popupBackground">@drawable/menu_dropdown_panel_customaction</item>
    </style>

    <style name="DropDownListView.Customaction" parent="@style/Widget.AppCompat.ListView.DropDown">
        <item name="android:listSelector">@drawable/selectable_background_customaction</item>
    </style>

    <style name="ActionBarTabStyle.Customaction" parent="@style/Widget.AppCompat.ActionBar.TabView">
        <item name="android:background">@drawable/tab_indicator_ab_customaction</item>
    </style>

    <style name="DropDownNav.Customaction" parent="@style/Widget.AppCompat.Spinner.DropDown.ActionBar">
        <item name="android:background">@drawable/spinner_background_ab_customaction</item>
        <item name="android:popupBackground">@drawable/menu_dropdown_panel_customaction</item>
        <item name="android:dropDownSelector">@drawable/selectable_background_customaction</item>
    </style>

    <style name="ProgressBar.Customaction" parent="@style/Widget.AppCompat.ProgressBar.Horizontal">
        <item name="android:progressDrawable">@drawable/progress_horizontal_customaction</item>
    </style>

    <style name="ActionButton.CloseMode.Customaction" parent="@style/Widget.AppCompat.ActionButton.CloseMode">
        <item name="android:background">@drawable/btn_cab_done_customaction</item>
    </style>

    <!-- this style is only referenced in a Light.DarkActionBar based theme -->
    <style name="Theme.Customaction.Widget" parent="@style/Theme.AppCompat">
        <item name="popupMenuStyle">@style/PopupMenu.Customaction</item>
        <item name="dropDownListViewStyle">@style/DropDownListView.Customaction</item>
    </style>

</resources>

这是我在MainActivity中的一部分:

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        super.onCreate(savedInstanceState);


        //Make sure you find out why it appears after a whole 1 second after the app appears
        SpannableString s = new SpannableString("GetDisciplined");
        s.setSpan(new TypefaceSpan(this, "roboto-lightitalic.ttf"), 0, s.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

// Update the action bar title with the TypefaceSpan instance
        ActionBar actionBar = getActionBar();
        actionBar.setTitle(s);
        setContentView(R.layout.merge);

我只是想通过让它立即显示来解决自定义ActionBar字体在一秒钟后出现的问题,但我不确定是否实现了这一点。

1 个答案:

答案 0 :(得分:0)

你可以做到的一种方式(不是很好的方式)是使用标题栏的自定义布局,你可以使用自定义文本视图来显示标题。

public class CustomTextView extends TextView {
private static Typeface customFont = null;

public CustomTextView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    if (isInEditMode()) {
        return;
    }

    if (customFont == null) {
        customFont = Typeface.createFromAsset(context.getApplicationContext().getAssets(),
                "fonts/custom_font.ttf");
    }
    setTypeface(customFont);
}

}

在您的活动中:

 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
 setContentView(R.layout.activities);
 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);

布局中的标题使用CustomTextView来保存标题。

再次,相当混乱的解决方案,但会做你想要的。