原生toolbar基本使用教程
1.先写布局文件
<android.support.v7.widget.Toolbar android:id="@+id/toolbar" app:title="12312" app:navigationIcon="@drawable/icon_back_32px" android:minHeight="?attr/actionBarSize" android:background="?attr/colorPrimary" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.Toolbar>
2.修改布局文件中的一些样式,在res->style下面
<!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat"> <!-- Customize your theme here. --> <!--toolbar的背景色--> <item name="colorPrimary">@color/colorPrimary</item> <!--toolbar上面的背景颜色--> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <!--toolbar的文字颜色--> <item name="android:textColorPrimary">@color/white</item> <item name="android:windowBackground">@android:color/white</item> <item name="android:windowActionBar">false</item> <item name="android:windowNoTitle">true</item> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style>
3.到mainactivity中监听事件
private android.support.v7.widget.Toolbar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar =findViewById(R.id.toolbar); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Show.alert("ASD"); } }); toolbar.inflateMenu(R.menu.menu_main); toolbar.setOnMenuItemClickListener(new android.support.v7.widget.Toolbar.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { int id =item.getItemId(); if(id==R.id.action_setting){ Show.alert("点击了设置"); return true; } return false; } }); initTab(); }
4.完成的效果如下面