问题
I have a typeface, I want to change the font of the action-bar title in android. Is there a way to set title typeface like that?
this.setTitle(myTitle.toUpperCase());
this.setTypefaceofTitle(tf);
this is not a copy question, those methods on this link (How to Set a Custom Font in the ActionBar Title?) are not working. When I try them, eclipse gives that error : java.lang.noSuchMethodError
回答1:
Try this,
int actionBarTitle = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
TextView actionBarTitleView = (TextView) getWindow().findViewById(actionBarTitle);
Typeface robotoBoldCondensedItalic = Typeface.createFromAsset(getAssets(), "fonts/Roboto-BoldCondensedItalic.ttf");
if(actionBarTitleView != null){
actionBarTitleView.setTypeface(robotoBoldCondensedItalic);
}
If it's toolbar means try like as below.
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="@color/action_color”
app:theme="@style/ToolBarTheme" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:layout_gravity="center"
android:id="@+id/title_txt” />
</android.support.v7.widget.Toolbar>
Then just add any style via programmatically using below comment.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
TextView mTitle = (TextView) toolbar.findViewById(R.id.title);
OR
Change using style. Need one info click here.
回答2:
I found this answer from headsvk that helped me. Doing some adjustments I only needed to do this:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
...
TextView myTitle = (TextView) toolbar.getChildAt(0);
myTitle.setTypeface(font);
Doing some tests on my app I found that the title TextView was the first child of the toolbar, so now that I have it I can set changes to it.
回答3:
https://stackoverflow.com/a/8748802/1943671 you can do like in this answer.
After setting your custom view to the Action Bar like in the link, just give the TextView the typeface:
Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/yourfont.ttf");
TextView tv = (TextView) findViewById(R.id.customTitle);
tv.setTypeface(tf);
回答4:
You can do that by creating a custom view and inflating that custom view in the ActionBar.
This custom view will contain a TextView
that you'll get a reference to, and then be able to set a typeface.
See an example here: How to Set a Custom Font in the ActionBar Title?
回答5:
It is not supported.But you can create custom view for your action bar. How to Set a Custom Font in the ActionBar Title?
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//getMenuInflater().inflate(R.menu.activity_main, menu);
this.getActionBar().setDisplayShowCustomEnabled(true);
this.getActionBar().setDisplayShowTitleEnabled(false);
Typeface tf = Typeface.createFromAsset(getAssets(),
"fonts/architectsdaughter.ttf");
LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.xml_master_footer, null);
this.getActionBar().setCustomView(v);
((TextView)v.findViewById(R.id.title)).setText(this.getTitle());
((TextView) v.findViewById(R.id.title)).setTypeface(tf);
//assign the view to the actionbar
return true;
}
and in your menifest
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="16" />
来源:https://stackoverflow.com/questions/17966350/changing-the-font-of-the-application-title-in-android