English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Detailed explanation of the usage method of Android Toolbar integration example

In the recent project, I encountered a situation where the Toolbar was different for different interfaces, so as to describe a unified style. I believe everyone is also very clear that most Toolbars include the following aspects:

  • Left title Left title color Left title icon, etc.
  • Title Title color
  • Right title Right title color Right title icon
  • ToolBar title ToolBar color ToolBar icon
  • ToolBar subtitle ToolBar subtitle ToolBar subtitle color

Let's take a look at the style interface of the appToolBar of Taobao and other apps.

Let's take a look at the CustomeToolBar I customized, which inherits from the original Toolbar.

package com.ldm.imitatewx;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
import android.widget.Toolbar;
/**
 * Created by John on 2017/2/16.
 */
public class CustomeToolBar extends Toolbar {
 private TextView mTvMainTitleLeft;
 private TextView mTvMainTitle;
 private TextView mTvMainRight;
 public CustomeToolBar(Context context) {
 super(context);
 }
 public CustomeToolBar(Context context, AttributeSet attrs) {
 super(context, attrs);
 }
 public CustomeToolBar(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 }
 @Override
 protected void onFinishInflate() {
 super.onFinishInflate();
 mTvMainTitleLeft = (TextView) findViewById(R.id.lt_main_title_left);
 mTvMainTitle = (TextView) findViewById(R.id.lt_main_title);
 mTvMainRight = (TextView) findViewById(R.id.lt_main_title_right);
 }
 //Set the main title content
 public void setMainTitle(String text)
 {
 this.setTitle(" ");
 mTvMainTitle.setVisibility(View.VISIBLE);
 mTvMainTitle.setText(text);
 }
 //Set the color of the main title content text
 public void setTitleColor(int color)
 {
 mTvMainTitle.setTextColor(color);
 }
 //Set the content of the left title
 public void setMainTitleLeft(String text)
 {
 mTvMainTitleLeft.setVisibility(View.VISIBLE);
 mTvMainTitleLeft.setText(text);
 }
 //Set the color of the left title
 public void setMainTitleLeftColor(int color)
 {
 mTvMainTitleLeft.setTextColor(color);
 }
 //Set left icon
 public void setMainTitleLeftDrawable(int res)
 {
 Drawable left = ContextCompat.getDrawable(getContext(), res);
 left.setBounds(0,0,left.getMinimumWidth(),left.getMinimumHeight());
 mTvMainTitleLeft.setCompoundDrawables(left,null,null,null);
 }
 //Set the right title
 public void setTvMainRightText(String text )
 {
 mTvMainRight.setVisibility(View.VISIBLE);
 mTvMainRight.setText(text);
 }
 //Set the color of the right title
 public void setMainTitleRightColor(int color )
 {
 mTvMainRight.setTextColor(color);
 }
 //Set the right icon
 public void setMainTitleRightDrawable(int res )
 {
 Drawable right= ContextCompat.getDrawable(getContext(),res);
 right.setBounds(0,0,right.getMinimumWidth(),right.getMinimumHeight());
 mTvMainTitleLeft.setCompoundDrawables(right,null,null,null);
 }
 //Set the toolbar color
 public void setToolBarBackground(int res )
 {
 this.setBackgroundResource(res);
 }
 //Set the icon on the left side of ToolBar
 public void setToolbarLeftBackImageRes(int res )
 {
 this.setNavigationIcon(res);
 }
 //Set toolbar left text
 public void setToolbarLeftText(String text ){
 this.setNavigationContentDescription(text);
 }
 //Set toolbar title
 public void setToolbarTitle(String text )
 {
 this.setTitle(text);
 }
 //Set the toolbar color
 public void setToolbarTitleColor(int color )
 {
 this.setTitleTextColor(color);
 }
 //Set the toolbar subtitle
 public void setToolbarSubTitleText(String text )
 {
 this.setSubtitle(text);
 }
 //Set the color of the toolbar subtitle
 public void setToolbarSubTitleTextColor(int color )
 {
 this.setSubtitleTextColor(color);
 }
}

Then the layout refers to activity_custome_toolbar
Because in fact, toolbar is also a view, and can also be said to be a layout
So we just need to put things in it according to our own needs, this may not be comprehensive, and I hope everyone can improve it together, thank you!

<?xml version="1.0" encoding="utf-8"?>
<com.ldm.imitatewx.CustomeToolBar 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="&#"63;attr/label_textSize"
 android:background="@android:color"/holo_green_light"
 android:fitsSystemWindows="true"
 app:contentInsetLeft="0dp"
 app:contentInsetStart="0dp"
 app:popupTheme="@style"/MyPopStyle"
 >
 <TextView
 android:id="@"+id/lt_main_title_left"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginLeft=""10dp"
 android:text="Return"
 android:gravity="center"
 android:drawableLeft="@drawable"/ic_back_u"
 android:textColor="@android:color"/white"
 android:singleLine="true"
 android:textSize="16sp"
 android:visibility="visible"/>
 <TextView
 android:id="@"+id/lt_main_title"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:singleLine="true"
 android:textColor="@android:color"/white"
 android:text="Title"
 android:textSize="20sp"
 android:visibility="visible"
 />
 <TextView
 android:id="@"+id/lt_main_title_right"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="right"
 android:layout_marginRight="10dp"
 android:text="Return"
 android:gravity="center"
 android:drawableRight="@drawable"/ic_add"
 android:textColor="@android:color"/white"
 android:singleLine="true"
 android:textSize="16sp"
 android:visibility="visible"/>
</com.ldm.imitatewx.CustomeToolBar>

That's about it! Everyone can continue to improve! Thank you!

That's all for this article. I hope it will be helpful to everyone's learning, and I also hope everyone will support the呐喊 tutorial more.

Statement: The content of this article is from the Internet, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to notice#w3Please send an email to codebox.com (replace # with @ when sending email) to report violations, and provide relevant evidence. Once verified, this site will immediately delete the suspected infringing content.

You May Also Like