Detailed use of Android custom ToolBar

Source: Internet
Author: User

Detailed use of Android custom ToolBar

Customize the ToolBar in xml, extend the options through the menu file, and use

1. ToolBar Layout

<? Xml version = "1.0" encoding = "UTF-8"?>
<Android. support. v7.widget. Toolbar xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: app = "http://schemas.android.com/apk/res-auto"
Android: id = "@ + id/toolbar"
Android: layout_width = "match_parent"
Android: layout_height = "? Attr/actionBarSize"
App: popupTheme = "@ style/ThemeOverlay. AppCompat. Light"
Android: focusable = "true"
Android: focusableInTouchMode = "true"
Android: background = "@ color/color_bg_toolbar">
<ImageView
Android: id = "@ + id/title_back"
Android: layout_width = "wrap_content"
Android: layout_height = "match_parent"
Android: visibility = "gone"
Android: src = "@ mipmap/ic_back"/>
<TextView
Android: id = "@ + id/title_name"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: singleLine = "true"
Android: layout_gravity = "center_horizontal"
Android: textSize = "20sp"
Android: textColor = "@ color/color_bg_write"
Android: text = "@ string/app_name"/>
<TextView
Android: id = "@ + id/title_rightTv"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: singleLine = "true"
Android: layout_marginRight = "10dp"
Android: layout_gravity = "right"
Android: textColor = "@ color/color_bg_write"
Android: text = "@ string/app_name"
Android: visibility = "gone"/>
</Android. support. v7.widget. Toolbar>

2. menu file (icon on the right of the title, test found that only three can be displayed, if more than three third will become more options ), the other way is to write in the Toolbar label in the xml layout as above

<? Xml version = "1.0" encoding = "UTF-8"?>
<Menu xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: app = "http://schemas.android.com/apk/res-auto">
<! -- Scan -->
<Item android: id = "@ + id/title_scan" // id
Android: title = "@ string/scan" // title long-pressed text
Android: visible = "false" // not displayed by default
Android: icon = "@ mipmap/ic_scan" // icon
App: showAsAction = "ifRoom"/>
<! -- Add -->
<Item android: id = "@ + id/title_add"
Android: title = "@ string/add"
Android: icon = "@ mipmap/ic_add"
App: showAsAction = "ifRoom"/>
<! -- Set -->
<Item android: id = "@ + id/title_setting"
Android: title = "@ string/setting"
Android: visible = "false"
Android: icon = "@ mipmap/ic_setting"
App: showAsAction = "ifRoom"/>
<! -- Search -->
<Item android: id = "@ + id/title_search"
Android: title = "@ string/search"
Android: visible = "false"
Android: icon = "@ mipmap/ic_search"
App: showAsAction = "ifRoom"/>
<! -- Date -->
<Item android: id = "@ + id/title_date"
Android: title = "@ string/date"
Android: visible = "false"
Android: icon = "@ mipmap/ic_calendar"
App: showAsAction = "ifRoom"/>
<! -- Refresh -->
<Item android: id = "@ + id/title_refresh"
Android: title = "@ string/refresh"
Android: visible = "false"
Android: icon = "@ mipmap/botton_refresh"
App: showAsAction = "ifRoom"/>
<! -- <Item android: id = "@ + id/title_more1" -->
<! -- Android: title = "@ string/right2" -->
<! -- App: showAsAction = "never"/> -->
<! -- <Item android: id = "@ + id/title_more2" -->
<! -- Android: title = "@ string/right2" -->
<! -- App: showAsAction = "never"/> -->
</Menu>

3. BaseActivity

/**
* Created by Administrator on 0005.
*/

Public class BaseActivity extends AppCompatActivity implements View. OnClickListener, Toolbar. OnMenuItemClickListener {
/* Toolbar */
Private Toolbar toolBar;
/**/
Private CoordinatorLayout coordinatorLayout;
/* Whether to load the icon for the first time (mainly for one-to-multiple fragment on the homepage )*/
Private boolean title_menu_first = true;
/* Whether to return the first load */
Private boolean title_back_first = true;
/* Whether it is returned (it may represent another function )*/
Private boolean is_title_back = true;
/* Return */
Private ImageView titleBack;
/* Title name */
Private TextView titleName;
@ Override
Protected void onCreate (@ Nullable Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// Portrait Screen
SetRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_PORTRAIT );
SetRootView ();
// CoordinatorLayout = getView (R. id. coordinatorLayout );
InitToolbar ();
InitWidght ();
}
Protected void setRootView (){}
Protected void initWidght (){};
//
Protected <T extends View> T getView (int resourcesId ){
Return (T) findViewById (resourcesId );
}/* Initialize toolbar */
Private void initToolbar (){
ToolBar = getView (R. id. toolbar );
ToolBar. setTitle ("");
ToolBar. setTitleTextColor (Color. WHITE );
TitleName = getView (R. id. title_name );
// Setsuppactionactionbar (toolBar );
}
/**
* Set return
* @ Param back: whether to return: yes --> return. If not, set other icons.
* @ Param resourcesId: icon id. this parameter is set at will when the returned result is returned, and is not used.
**/
Protected void setTitleBack (final boolean back, int resourcesId ){
Is_title_back = back;
If (title_back_first | titleBack = null ){
TitleBack = getView (R. id. title_back );
TitleBack. setOnClickListener (this );
Title_back_first = false;
}
TitleBack. setVisibility (View. VISIBLE );
If (! Back ){
TitleBack. setImageResource (resourcesId );
}
}
/**
* Set title
* @ Param title: title
**/
Protected void setTitleName (String title ){
TitleName. setText (title );
}
/**
* Right side of title: icon class
**/
Protected void setRightRes (){
// Extended menu
ToolBar. inflateMenu (R. menu. base_toolbar_menu );
// Add a listener
ToolBar. setOnMenuItemClickListener (this );
}
/**
* Display the title icon
* @ Param itemId: Option id corresponding to the icon (one to three). A maximum of three options can be displayed.
**/
Protected void showTitleRes (int... itemId ){
If (title_menu_first ){
SetRightRes ();
Title_menu_first = false;
}
For (int item: itemId ){
// Display
ToolBar. getMenu (). findItem (item). setVisible (true); // you can use the id to search for it. You can also use setIcon () to set the icon.
// ToolBar. getMenu (). getItem (0). setVisible (true); // locate by location
}
}
/**
* Hide the title icon
* @ Param itemId: Option id corresponding to the icon
**/
Protected void goneTitleRes (int... itemId ){
If (titleBack! = Null)
TitleBack. setVisibility (View. GONE );
For (int item: itemId ){
// Hide
ToolBar. getMenu (). findItem (item). setVisible (false );
}
}
/**
* Text to the right of title
* @ Param str: Text Content
**/
Protected void setTitleRightText (String str ){
TextView textView = getView (R. id. title_rightTv );
TextView. setVisibility (View. VISIBLE );
TextView. setText (str );
TextView. setOnClickListener (this );
}
@ Override
Public void onClick (View v ){
If (v. getId () = R. id. title_back & is_title_back ){
OnBackPressed ();
}
}
// Toolbar menu listening
@ Override
Public boolean onMenuItemClick (MenuItem item ){
Return false;
}
}

3. Activity usage

Public class MainActivity extends BaseActivity {
@ Override
Protected void setRootView (){
Super. setRootView ();
SetContentView (R. layout. activity_main );
}
@ Override
Protected void initWidght (){
Super. initWidght ();
SetTitleName (getResources (). getString (R. string. chat); // title
// SetTitleBack (false, R. mipmap. ic_search); // There are icons, but not returned
// SetTitleBack (true, 0); // return
// SetTitleRightText ("save"); // The right text
ShowTitleRes (R. id. title_add, R. id. title. setting); // expand menu (icon)
// GoneTitleRes (R. id. title_add); hides the icon, which is generally unavailable.
}
@ Override
Public void onClick (View v ){
If (v. getId () = R. id. title_back ){
WorkFragment. titleSearch ();
}
}
@ Override
Public boolean onMenuItemClick (MenuItem item ){
Switch (item. getItemId ()){
Case R. id. title_add: // Add
Break;
Case R. id. title_apps: // application break;
Case R. id. title_setting: // set
Break;
}
Return super. onMenuItemClick (item );
}
}

Other Toolbar settings

1. Remove the left blank and set it in the style file.

<Style name = "AppTheme" parent = "Theme. AppCompat. Light. NoActionBar">
<! -- Customize your theme here. -->
<Item name = "colorPrimary"> @ color/colorAppTheme </item>
<Item name = "colorPrimaryDark"> @ color/colorAppTheme </item>
<Item name = "col1_cent"> @ color/col1_cent </item>
<Item name = "toolbarStyle" tools: ignore = "NewApi"> @ style/MyToolBar </item>
</Style>
<Style name = "MyToolBar" parent = "Widget. AppCompat. Toolbar">
<! -- Left margin -->
<Item name = "contentInsetStart"> 0dp </item>
</Style>

Https://www.bkjia.com/topicnews.aspx? Tid = 11

This article permanently updates link: https://www.bkjia.com/Linux/2018-03/151421.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.