Android toolbar center title and custom font

前端 未结 30 2694
时光说笑
时光说笑 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:05

    I don't know if anything changed in the appcompat library but it's fairly trivial, no need for reflection.

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    
    // loop through all toolbar children right after setting support 
    // action bar because the text view has no id assigned
    
    // also make sure that the activity has some title here
    // because calling setText() with an empty string actually
    // removes the text view from the toolbar
    
    TextView toolbarTitle = null;
    for (int i = 0; i < toolbar.getChildCount(); ++i) {
        View child = toolbar.getChildAt(i);
    
        // assuming that the title is the first instance of TextView
        // you can also check if the title string matches
        if (child instanceof TextView) {
            toolbarTitle = (TextView)child;
            break;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 05:05

    I was facing the same issue, fixed by doing this in MainActivity

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    

    And In Fragment

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            // Inflate the layout for this fragment
            view = inflater.inflate(R.layout.fragment_example, container, false);
            init();
        }
        getActivity().setTitle("Choose Fragment");
        return view;
    }
    
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.example_menu, menu);
    }
    
    0 讨论(0)
  • 2020-11-22 05:08

    I found another way to add custom toolbar without any adicional Java/Kotlin code.

    • First: create a XML with your custom toolbar layout with AppBarLayout as the parent:

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.design.widget.AppBarLayout                     
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:theme="@style/AppTheme.AppBarOverlay">
      
      <android.support.v7.widget.Toolbar
          android:id="@+id/toolbar"
          android:layout_width="match_parent"
          android:layout_height="?attr/actionBarSize"
          android:background="?attr/colorPrimary"
          app:popupTheme="@style/AppTheme.PopupOverlay">
      
          <ImageView
              android:layout_width="80dp"
              android:layout_height="wrap_content"
              android:layout_gravity="right"
              android:layout_marginEnd="@dimen/magin_default"
              android:src="@drawable/logo" />
      
      </android.support.v7.widget.Toolbar>
      

    • Second: Include the toolbar in your layout:

      <?xml version="1.0" encoding="utf-8"?>                
      <android.support.constraint.ConstraintLayout 
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@color/blue"
          tools:context=".app.MainAcitivity"
          tools:layout_editor_absoluteY="81dp">
      
          <include
              layout="@layout/toolbar_inicio"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent" />
      
          <!-- Put your layout here -->
      
      </android.support.constraint.ConstraintLayout>
      
    0 讨论(0)
  • 2020-11-22 05:09

    To use a custom title in your Toolbar all you need to do is remember is that Toolbar is just a fancy ViewGroup so you can add a custom title like so:

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_top"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="@color/action_bar_bkgnd"
        app:theme="@style/ToolBarTheme" >
    
    
         <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Toolbar Title"
            android:layout_gravity="center"
            android:id="@+id/toolbar_title" />
    
    
        </android.support.v7.widget.Toolbar>
    

    This means that you can style the TextView however you would like because it's just a regular TextView. So in your activity you can access the title like so:

    Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
    TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title);
    
    0 讨论(0)
  • 2020-11-22 05:09

    No one has mentioned this, but there are some attributes for Toolbar:

    app:titleTextColor for setting the title text color

    app:titleTextAppearance for setting the title text appearance

    app:titleMargin for setting the margin

    And there are other specific-side margins such as marginStart, etc.

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

    I solved this solution , And this is a following codes:

    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" >
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Order History"
                android:layout_gravity="center"
                android:id="@+id/toolbar_title"
                android:textSize="17sp"
                android:textStyle="bold"
                android:textColor="@color/colorWhite"
                />
    
        </android.support.v7.widget.Toolbar>
    

    And you can change title/label , in Activity, write a below codes:

    Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
    

    TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title); mTitle.setText("@string/....");

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