android Toolbar Use

1, the navigation controller:
   Google to unify navigation android3.0 out ActionBar, ActionBar use more strenuous
   JakeWharton
ActionBarSherlock a lot of people use it
later android launched android.support.v7.widget.Toolbar

  ToolBar functions: management title, back button, menu

2. Use the toolbar

2.1 Add dependence

 compile 'com.android.support:design:27.1.1'

2.2 Layout 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context="mk.denganzhi.com.cehua.MainActivity"
    android:orientation="vertical">

    <!-- 定义toolbar控件,navigationIcon 定义返回按键 -->
    <android.support.v7.widget.Toolbar
        android:id="@+id/mytoolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:title="网易新闻"
        app:subtitle="新闻"
        android:background="?attr/colorPrimary"
        app:logo="@mipmap/ic_launcher"
        app:navigationIcon="@mipmap/add_pwd_left"
        >


    </android.support.v7.widget.Toolbar>
    
</LinearLayout>

2.3. Setting topic 

  <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

2.4. Activity added to the toolbar 

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ///使用Toolbar代码ActionBar
        Toolbar toolbar= findViewById(R.id.mytoolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}

Renderings:

Published 97 original articles · won praise 100 · views 130 000 +

Guess you like

Origin blog.csdn.net/dreams_deng/article/details/104970554