lBar简介
ToolBar是Android 5.0推出的一个新的导航控件用于取代之前的ActionBar,由于其高度的可定制性、灵活性、具有Material Design风格等优点,越来越多的应用也用上了ToolBar,比如常用的知乎软件其顶部导航栏正是使用ToolBar。官方考虑到仍有一部分用户的手机版本号低于5.0,所以,ToolBar也放进了support v7包内,使得低版本的系统也能使用上ToolBar。本文将使用support v7支持包的ToolBar来进行讲解,包括其基本用法、样式定制等知识点。
使用方式
1,引入support v7支持包
在你项目的build.gradle内输入如下代码,即能引入支持包,该支持包内有能向下兼容的ToolBar:
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.3.1' //h testCompile 'junit:junit:4.12' }
2,在布局中添加
`
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:tollbar="http://schemas.android.com/apk/res-auto" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="app.com.mytoolbardemo.MainActivity"> <!--popupTheme设置menu的字体颜色--> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/colorAccent" android:titleTextColor="@color/colorWhite" tollbar:popupTheme="@style/Theme.ToolBar.Zhifu" > <!--自定义控件--> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Clock" android:layout_gravity="center" android:gravity="center"/> </android.support.v7.widget.Toolbar> <LinearLayout android:layout_below="@+id/toolbar" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/app_name" android:textSize="15dp"/> </LinearLayout> </RelativeLayout>
`
3,在activity进行设置
public class MainActivity extends AppCompatActivity { private Toolbar toolbar ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); supportRequestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); initView (); } private void initView(){ toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setNavigationIcon(R.mipmap.ic_launcher);//设置导航栏图标 // toolbar.setLogo(R.mipmap.ic_launcher);//设置app logo toolbar.setTitle("Title");//设置主标题 toolbar.setTitleTextColor(getResources().getColor(R.color.colorWhite)); // toolbar.setSubtitle("Subtitle");//设置子标题 toolbar.inflateMenu(R.menu.menu_main);//设置右上角的填充菜单 toolbar.setOnMenuItemClickListener(onMenuItemClick); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "setNavigationIcon", Toast.LENGTH_SHORT).show(); } }); } private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem menuItem) { String msg = ""; switch (menuItem.getItemId()) { case R.id.action_edit: msg += "Click edit"; break; case R.id.action_share: msg += "Click share"; break; case R.id.action_settings: msg += "Click setting"; break; } if(!msg.equals("")) { Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show(); } return true; } }; }