Changing the font of the application title in android

前端 未结 5 1770
借酒劲吻你
借酒劲吻你 2021-01-03 08:53

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.toUppe         


        
相关标签:
5条回答
  • 2021-01-03 09:24

    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" />
    
    0 讨论(0)
  • 2021-01-03 09:25

    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);
    
    0 讨论(0)
  • 2021-01-03 09:37

    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.

    0 讨论(0)
  • 2021-01-03 09:46

    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?

    0 讨论(0)
  • 2021-01-03 09:50

    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.

    0 讨论(0)
提交回复
热议问题