Android-toolbar navigation Style

Source: Internet
Author: User

1. Modify the title margin

Modify the margin using the system's app properties to introduce use, i.e.:

  xmlns:app="http://schemas.android.com/apk/res-auto"
    • 1

Like what:

<Android.support.v7.widget.Toolbarxmlns:app= "http://schemas.android.com/apk/res-auto" android:id="@+id/toolbar" android:layout _width= "match_parent" android:layout_height="wrap_content" android:layout_marginbottom="1DP" android:background="? attr/colorprimary" android:minheight= "? attr/actionbarsize" Android : orientation= "vertical" app:contentinsetleft="10DP"/>      

Of course, can also be modified in the style.xml, their own research it;

2. Modify the padding value of navigation icon

Modify the padding value, you need to modify it in Style.xml, and here we modify the pading value of navigation:

Widget.AppCompat.Toolbar.Button.Navigation

Like what:

1. Define Style

    <style name= " Mytoolbarnavigationbuttonstyle "parent=" @style/ Widget.AppCompat.Toolbar.Button.Navigation "> <item name= "android:minwidth" >0dp</item> <item name= " android:p adding ">@dimen/margin_horizontal_large</item> <item name=" Android: ScaleType ">centerInside</item> </STYLE>   
    • 1

Apply in 2.app Themes

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="toolbarNavigationButtonStyle">@style/myToolbarNavigationButtonStyle</item> </style>
    • 1
3.title Center

Toolbar can be customized layout, you can add a TextView in the toolbar to achieve, in lieu of title;
Like what:

1. Layout

<?xml version= "1.0" encoding= "Utf-8"?><Android.support.v7.widget.ToolbarAndroid:id="@+id/toolbar"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="? Actionbarsize"Android:background= "@null" app:popuptheme= "@style/ ThemeOverlay.AppCompat.Light "app:theme=" @style/apptheme " > <textview android:id= "@+id/toolbar_title" style= "@style/ TextAppearance.AppCompat.Widget.ActionBar.Title "android:layout_width=" wrap_content "android:layout_height=" Wrap_ Content "android:layout_gravity=" center "/></ANDROID.SUPPORT.V7.WIDGET.TOOLBAR>     

2. Initialization

View v = findViewById(R.id.toolbar);if (v != null) {    toolbar = (Toolbar) v;    setSupportActionBar(toolbar);    toolbarTitle = (TextView) v.findViewById(R.id.toolbar_title);    if (toolbarTitle != null) {        getSupportActionBar().setDisplayShowTitleEnabled(false); }}

3. Use
Set the title two ways:
(1) directly in SetText ();
(2) Designation of title in Androidmanifest.xml;
(3) If there is baseactivity, the solution is as follows:
Activity has a ontitlechanged interface, which is called in the activity's onpostcreate and settitle;

protected void onPostCreate(@Nullable Bundle savedInstanceState) {    if (!isChild()) {        mTitleReady = true; onTitleChanged(getTitle(), getTitleColor()); } mCalled = true;}

So just overload the method in Baseactivity, as shown below:

@Overrideprotected void onTitleChanged(CharSequence title, int color) { super.onTitleChanged(title, color); if (toolbarTitle != null) { toolbarTitle.setText(title); }}

Reference-point Me

4. Modify the padding value of the menu

Define the style value:

    name="mToolbarStyle" parent="@style/Widget.AppCompat.Toolbar">        <item name="android:paddingRight">0dp</item> <item name="android:paddingLeft">13dp</item> </style>
    • 1

Use:

<android.support.v7.widget.Toolbar    android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="1dp" android:background="?attr/colorPrimary" android:minHeight="?attr/actionBarSize" android:orientation="vertical" style="@style/mToolbarStyle"/>
    • 1

The above method, on some machines will be invalid, we have the above approach, please tell me, thank you!

5. Reference

Modify some properties of toolbar
Toolbar learning to use

Android-toolbar navigation Style

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.