Custom font for ActionBarSherlock tabs

后端 未结 6 2024
滥情空心
滥情空心 2020-12-16 02:13

I want to set font for the \"Video\" and \"Image\" tabs in ActionBarSherlock. I have used the following code to do so. Its showing accurately in ICS but not in

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-16 02:28

    You can do it with this:

    // Set a custom font on all of our ActionBar Tabs
    private boolean setCustomFontToActionBarTab(Object root) {
    
        // Found the container, that holds the Tabs. This is the ScrollContainerView to be specific,
        // but checking against that class is not possible, since it's part of the hidden API.
        // We will check, if root is an instance of HorizontalScrollView instead,
        // since ScrollContainerView extends HorizontalScrollView.
        if (root instanceof HorizontalScrollView) {
            // The Tabs are all wraped in a LinearLayout
            root = ((ViewGroup) root).getChildAt(0);
            if (root instanceof LinearLayout) {
                // Found the Tabs. Now we can set a custom Font to all of them.
                for (int i = 0; i < ((ViewGroup) root).getChildCount(); i++) {
                    LinearLayout child = ((LinearLayout)((ViewGroup) root).getChildAt(i));
                    TextView actionBarTitle = (TextView) child.getChildAt(0);
                    Typeface tf = Typeface.createFromAsset(mContext.getAssets(), "font/font.ttf");
                    actionBarTitle.setTypeface(tf)
                }
                return true;
            }
    
        } 
        // Search ActionBar and the Tabs. Exclude the content of the app from this search.
        else if (root instanceof ViewGroup) {
            ViewGroup group = (ViewGroup) root;
            if (group.getId() != android.R.id.content) {
                // Found a container that isn't the container holding our screen layout.
                // The Tabs have to be in here.
                for (int i = 0; i < group.getChildCount(); i++) {
                    if (setCustomFontToActionBarTab(group.getChildAt(i))) {
                        // Found and done searching the View tree
                        return true;
                    }
                }
            }
        }
        // Found nothing
        return false;
    }
    

    Call it with this:

    ViewParent root = findViewById(android.R.id.content).getParent();
    setCustomFontToActionBarTab(root);
    

提交回复
热议问题