Android toolbar center title and custom font

前端 未结 30 2696
时光说笑
时光说笑 2020-11-22 04:11

I\'m trying to figure out the right way to use a custom font for the toolbar title, and center it in the toolbar (client requirement).

At the moment, i\'m using the

相关标签:
30条回答
  • 2020-11-22 05:11

    You can insert this code in your xml file

     <androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Toolbar Title"
            android:textColor="#000000"
            android:textSize="20dp"
            android:id="@+id/toolbar_title" />
    
    </androidx.appcompat.widget.Toolbar>
    
    0 讨论(0)
  • 2020-11-22 05:12

    Setting android:gravity="center" worked for me

    No styling nothing. Toolbar is basically a ViewGroup all you need to do is set gravity of elements in it.

            <android.support.v7.widget.Toolbar
                android:id="@+id/htab_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_gravity="top"
                android:background="@color/partial_transparent"
                android:gravity="center"
                app:layout_collapseMode="pin"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    
    0 讨论(0)
  • 2020-11-22 05:13

    A very quick and easy way to set a custom font is to use a custom titleTextAppearance with a fontFamily:

    Add to styles.xml:

    <style name="ToolbarTitle" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
        <item name="android:textSize">16sp</item>
        <item name="android:textColor">#FF202230</item>
        <item name="android:fontFamily">@font/varela_round_regular</item>
    </style>
    

    In your res folder create a font folder (Ex: varela_round_regular.ttf)

    Read the official guide to find out more https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html

    0 讨论(0)
  • 2020-11-22 05:13

    Solution that I used for this problem:

     public static void applyFontForToolbarTitle(Activity a){
            Toolbar toolbar = (Toolbar) a.findViewById(R.id.app_bar);
            for(int i = 0; i < toolbar.getChildCount(); i++){
                View view = toolbar.getChildAt(i);
                if(view instanceof TextView){
                    TextView tv = (TextView) view;
                    if(tv.getText().equals(a.getTitle())){
                        tv.setTypeface(getRuneTypefaceBold(a));
                        break;
                    }
                }
            }
        }
    

    For center gravity I think it would be necessary to change layout params to match_parent horizontally and then:

    tv.setGravity(Gravity.CENTER);
    
    0 讨论(0)
  • 2020-11-22 05:13
    private void makeTitleCenter(String title, Toolbar toolbar) {
        if (title != null && !TextUtils.isEmpty(title.trim())) {
            final String tag = " ";
            if (getSupportActionBar() != null) {
                getSupportActionBar().setTitle(tag);
            }
            TextView titleTv = null;
            View leftBtn = null;
            for (int i = 0; i < toolbar.getChildCount(); i++) {
                View view = toolbar.getChildAt(i);
                CharSequence text = null;
                if (view instanceof TextView && (text = ((TextView) view).getText()) != null && text.equals(tag)) {
                    titleTv = (TextView) view;
                } else if (view instanceof ImageButton) {
                    leftBtn = view;
                }
            }
            if (titleTv != null) {
                final TextView fTitleTv = titleTv;
                final View fLeftBtn = leftBtn;
                fTitleTv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        fTitleTv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        int leftWidgetWidth = fLeftBtn != null ? fLeftBtn.getWidth() : 0;
                        fTitleTv.setPadding(DimenUtil.getResources().getDisplayMetrics().widthPixels / 2 - leftWidgetWidth - fTitleTv.getWidth() / 2, 0, 0, 0);
                        fTitleTv.requestLayout();
                    }
                });
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 05:13

    for custom font in toolbar you can override textView font in style and then every textView in your app also toolbar title font changed automatically i tested it in android studio 3.1.3

    in style do it:

    <style name="defaultTextViewStyle" parent="android:Widget.TextView">
            <item name="android:fontFamily">@font/your_custom_font</item>
    </style>
    

    and then in your theme use this:

    <item name="android:textViewStyle">@style/defaultTextViewStyle</item>
    
    0 讨论(0)
提交回复
热议问题