【问题标题】:How to make custom font in actionbar show up immediately如何使操作栏中的自定义字体立即显示
【发布时间】:2014-07-10 07:52:53
【问题描述】:

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

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

在cmets用户中,艺术品,说,

在启动应用的那一刻,默认的标题样式是可见的 大约 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 字体在一秒钟后显示的问题,但我不确定能否实现。

【问题讨论】:

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


    【解决方案1】:

    您可以做到这一点的一种方法(不是很好的方法)是为您的标题栏使用自定义布局,您可以在其中使用显示标题的自定义文本视图。

    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 来保存标题。

    再一次,非常混乱的解决方案,但会做你想做的事。

    【讨论】:

    • 为标题栏创建布局
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多