因为实际开发一个App需要用自定义的Toolbar/Actionbar ,网上查了很多资料发现写的并不全,今天通过实际编写代码总结出一个比较简单方便的自定义toolbar的方法,仅供参考
1,toolbar修改后退键图标的方法
获得Toolbar的实例,直接使用setNavigationIcon(resId)即可改变原来的后退按钮图标
toolbar.setNavigationIcon(R.drawable.
button_login)
;
2,自定义Toolbar的样式
例:将toolbar的title改到中间:
->MainActivity中取消显示Title
getSupportActionBar().setDisplayShowTitleEnabled(
false)
;
->在Toolbar内部加入TextView用来当作title显示,代码如下,我添加了一个TextView显示Title,一个ImageView显示刷新的图标
<android.support.v7.widget.Toolbar
android :id= "@+id/toolbar"
android :layout_width= "match_parent"
android :layout_height= "?attr/actionBarSize"
android :background= "@color/colorPrimary"
android :elevation= "4dp"
app :contentInsetLeft= "0dp"
app :popupTheme= "@style/Theme.AppCompat.Light"
app :contentInsetStart= "0dp"
android :theme= "@style/Theme.Design.NoActionBar" >
<TextView
android :id= "@+id/toolbar_title"
android :text= "@string/title_yun_note"
android :layout_gravity= "center"
android :textSize= "20sp"
android :textColor= "@android:color/white"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content" />
< ImageView
android :src= "@drawable/navigation_refresh"
android :id= "@+id/image_button_refresh"
android :layout_gravity= "right"
android :visibility= "invisible"
android :layout_marginRight= "5dp"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content" />
</android.support.v7.widget.Toolbar>
android :id= "@+id/toolbar"
android :layout_width= "match_parent"
android :layout_height= "?attr/actionBarSize"
android :background= "@color/colorPrimary"
android :elevation= "4dp"
app :contentInsetLeft= "0dp"
app :popupTheme= "@style/Theme.AppCompat.Light"
app :contentInsetStart= "0dp"
android :theme= "@style/Theme.Design.NoActionBar" >
<TextView
android :id= "@+id/toolbar_title"
android :text= "@string/title_yun_note"
android :layout_gravity= "center"
android :textSize= "20sp"
android :textColor= "@android:color/white"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content" />
< ImageView
android :src= "@drawable/navigation_refresh"
android :id= "@+id/image_button_refresh"
android :layout_gravity= "right"
android :visibility= "invisible"
android :layout_marginRight= "5dp"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content" />
</android.support.v7.widget.Toolbar>
->以上这种方式可以完全自定义toolbar的所有控件,自定义的控件可以通过MainActivity::toolbar.findViewById()设置事件的监听,代码如下
refreshButton = (ImageView)
toolbar.findViewById(R.id.
image_button_refresh)
;
refreshButton.setVisibility(View. VISIBLE) ;
refreshButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO: 16/8/13 rotate refresh button
// TODO: 16/8/13 check isLogin, if not, go to LoginActivity
Toast. makeText(MainActivity. this, "rotate refresh button" ,Toast. LENGTH_SHORT).show() ;
}
}) ;
refreshButton.setVisibility(View. VISIBLE) ;
refreshButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO: 16/8/13 rotate refresh button
// TODO: 16/8/13 check isLogin, if not, go to LoginActivity
Toast. makeText(MainActivity. this, "rotate refresh button" ,Toast. LENGTH_SHORT).show() ;
}
}) ;
3,以上的Toolbar自定义样式可以与res\menu\menu.xml 一起用,即
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu. menu ,menu) ;
return super.onCreateOptionsMenu(menu) ;
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu. menu ,menu) ;
return super.onCreateOptionsMenu(menu) ;
}
这样你的toolbar就可以自定义标题样式以及控件,同时又可以用overflow了。
关于Toolbar的基本使用请查看