问题描述
绝对Android开发的初学者在这里,所以请大家多多包涵。我想实现两件事情与我的动作条的标题:
Absolute Android development beginner here, so please bear with me. I am trying to achieve 2 things with my title in the ActionBar:
- 中心对齐标题文本
- 使用自定义字体标题文本
我试图按照this答案,所以请你看看这个答案第一。
I'm trying to follow this answer, so please look at that answer first.
内容的 MainActivity.java
// ATTEMPT TO STYLE THE ACTIONBAR
this.getActionBar().setDisplayShowCustomEnabled(true);
this.getActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.titleview, null);
//if you need to customize anything else about the text, do it here.
//I'm using a custom TextView with a custom font in my layout xml so all I need to do is set title
((TextView)v.findViewById(R.id.title)).setText(this.getTitle());
//assign the view to the actionbar
this.getActionBar().setCustomView(v);
我创建了一个新的XML文件:布局/ titleview.xml ,以符合 R.layout.titleview
上方。它包含:
I created a new XML file: layout/titleview.xml to correspond to R.layout.titleview
above. It contains:
<com.muppethead.app.name.CustomTextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:textSize="20dp"
android:maxLines="1"
android:ellipsize="end"
android:text="MY APP TITLE" />
这提供了错误:以下类不能被找到:com.muppethead.app.name.CustomTextView
问题
我在哪里去了?除了修复上面的错误,在这里我引用我的字体,它位于资产/字体/ font.otf
?又是怎么回事居中的文字?
Where am I going wrong? In addition to fixing the above error, where do I reference my font, which is located in assets/fonts/font.otf
? And what about centering the text?
我得说,可耻的是谷歌不使这一切成为可能从XML。它消除了可能性,新的开发人员能够创建什么吸引力。
I gotta say, shame on Google for not making this possible from the XML. It removes the possibility for new developers to create anything attractive.
在此先感谢您的帮助。
推荐答案
我就老老实实做的是忘掉定制的TextView
,并且只使用默认一个在XML。您使用的是仅此一次,并且只自定义字体。
What I would honestly do is forget about the custom TextView
, and just use the default one in the XML. You're using it just this once, and only for the custom font.
您可以做这样的事情,而不是:
You can do something like this instead:
TextView tv = (TextView) v.findViewById(R.id.title);
Typeface tf = Typeface.createFromFile(getAssets(), "fonts/font.otf");
tv.setTypeface(tf);
tv.setText(this.getTitle());
这篇关于中心对齐动作条的标题和放大器;使用自定义的字体,以及的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!