How to Set a Custom Font in the ActionBar Title?

后端 未结 17 1520
面向向阳花
面向向阳花 2020-11-22 09:46

How (if possible) could I set a custom font in a ActionBar title text(only - not the tab text) with a font in my assets folder? I don\'t want to use the android:logo option.

17条回答
  •  死守一世寂寞
    2020-11-22 10:13

    TRY THIS

    public void findAndSetFont(){
            getActionBar().setTitle("SOME TEST TEXT");
            scanForTextViewWithText(this,"SOME TEST TEXT",new SearchTextViewInterface(){
    
                @Override
                public void found(TextView title) {
    
                } 
            });
        }
    
    public static void scanForTextViewWithText(Activity activity,String searchText, SearchTextViewInterface searchTextViewInterface){
        if(activity == null|| searchText == null || searchTextViewInterface == null)
            return;
        View view = activity.findViewById(android.R.id.content).getRootView();
        searchForTextViewWithTitle(view, searchText, searchTextViewInterface);
    }
    
    private static void searchForTextViewWithTitle(View view, String searchText, SearchTextViewInterface searchTextViewInterface)
    {
        if (view instanceof ViewGroup)
        {
            ViewGroup g = (ViewGroup) view;
            int count = g.getChildCount();
            for (int i = 0; i < count; i++)
                searchForTextViewWithTitle(g.getChildAt(i), searchText, searchTextViewInterface);
        }
        else if (view instanceof TextView)
        {
            TextView textView = (TextView) view;
            if(textView.getText().toString().equals(searchText))
                if(searchTextViewInterface!=null)
                    searchTextViewInterface.found(textView);
        }
    }
    public interface SearchTextViewInterface {
        void found(TextView title);
    }
    

提交回复
热议问题