android - 使用 ActionBarsherlock 时更改应用程序标签字体

标签 android android-actionbar actionbarsherlock android-fonts

我正在尝试更改应用程序标签的字体(我在我的应用程序中使用 ActionBarSherlock 作为库项目)并出现错误。

以下是我尝试在 setContentView() 之后将存储在 Assets 中的外部字体设置为应用程序标签的代码 fragment 。

mAppName = (TextView) findViewById(R.id.abs__action_bar_title);
Typeface face = Typeface.createFromAsset(getAssets(), "fonts/handsean.ttf");
mAppName.setTypeface(face);

以下是来自 logcat 的错误。错误似乎出现在行 mAppName.setTypeface(face) 上。

E/AndroidRuntime(18762): FATAL EXCEPTION: main
E/AndroidRuntime(18762): java.lang.RuntimeException: Unable to start activity  ComponentInfo{com.cheats/com.cheats.LandingPage}: java.lang.NullPointerException
E/AndroidRuntime(18762):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
E/AndroidRuntime(18762):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
E/AndroidRuntime(18762):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
E/AndroidRuntime(18762):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
E/AndroidRuntime(18762):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(18762):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(18762):    at android.app.ActivityThread.main(ActivityThread.java:5039)
E/AndroidRuntime(18762):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(18762):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(18762):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime(18762):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime(18762):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(18762): Caused by: java.lang.NullPointerException
E/AndroidRuntime(18762):    at com.cheats.LandingPage.onCreate(LandingPage.java:89)
E/AndroidRuntime(18762):    at android.app.Activity.performCreate(Activity.java:5104)
E/AndroidRuntime(18762):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
E/AndroidRuntime(18762):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

我在 abs__action_bar_title_item.xml 中找到了应用程序标签的 TextView 的以下声明:

<TextView android:id="@+id/abs__action_bar_title"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:singleLine="true"
              android:ellipsize="end" />

这是正确的方法还是我遗漏了什么?有人可以建议吗?

感谢您的宝贵时间。

最佳答案

经过大量试验,我能够以下面的方式获取 textView 的引用,现在字体已经改变了:

int titleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
if(titleId == 0)
    titleId = com.actionbarsherlock.R.id.abs__action_bar_title;

mAppName = (TextView) findViewById(titleId);
Typeface face = Typeface.createFromAsset(getAssets(), "fonts/handsean.ttf");
mAppName.setTypeface(face);

关于android - 使用 ActionBarsherlock 时更改应用程序标签字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14795403/

相关文章:

android - 更改操作栏标题颜色

Android - 动画 Intent 但忽略 View

android - 获取 Toolbar 的导航图标 View 引用

android - SherlockFragment onCreateOptionsMenu 没有调用

android - SherlockActionBar : Export signed apk, 然后 Eclipse 崩溃:(

android - 应用广告中的 AdMob - 始终如一

android - Travis CI Android - 找不到匹配的版本

android - 在 ActionBar 上隐藏主页按钮 Sherlock Actionbar 扩展导航 Activity

android - 将不同的主题应用于操作栏

android - 复制操作栏嵌入式选项卡