Java 如何在actionbar中立即显示自定义字体
为了更改actionbar中的字体,我实现了一个自定义的Typeface span类,并在onCreate中使用了一个SpannableString 这就是我所做的: 用户artwork在评论中说 启动应用程序时,默认标题样式可见 大约1秒钟后,自定义样式出现 然后回答问题的人说 “这是因为您的活动主题正在应用于 在你的代码开始运行之前进行DecorView。你可以通过 将动作栏隐藏在主题中,然后将其显示为您的活动 正在创建。” 我不知道如何实现以下内容。我也不确定在我定制的动作栏中是否能做到这一点 这就是我设计actionbar的方式:Java 如何在actionbar中立即显示自定义字体,java,android,xml,android-layout,android-actionbar,Java,Android,Xml,Android Layout,Android Actionbar,为了更改actionbar中的字体,我实现了一个自定义的Typeface span类,并在onCreate中使用了一个SpannableString 这就是我所做的: 用户artwork在评论中说 启动应用程序时,默认标题样式可见 大约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>
我只是想通过使自定义ActionBar字体立即显示来解决一秒钟后显示的问题,但我不确定是否能实现这一点。一种方法是为标题栏使用自定义布局,其中使用显示标题的自定义文本视图
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来保存标题
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);
}
再一次,这是一个相当混乱的解决方案,但它可以满足您的需要。为标题栏创建一个布局