三步. 颜色相同的地方,互相对应.
1. 自定义View
public class CommonToolBar extends Toolbar {
private boolean ifShowBack;//是否显示返回按钮
public CommonToolBar(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.CommonToolBar); ifShowBack = a.getBoolean(R.styleable.CommonToolBar_ifShowBack,true);//第二部定义的attr,会自动生成CommonToolBar_ifShowBackif (ifShowBack) {setNavigationIcon(R.mipmap.icon_back); } a.recycle(); }}2.attrs.xml中定义attrs<resources><declare-styleable name="CommonToolBar"> <attr name="ifShowBack" format="boolean"/> </declare-styleable> </resources>3.布局文件xml中顶级节点增加: xmlns:frame="http://schemas.android.com/apk/res-auto"
自定义View增加:frame:ifShowBack="false"注意: 如果是TextView的文字大小,记得TypedValue.COMPLEX_UNIT_SP, TypedValue.COMPLEX_UNIT_PX, TypedValue.COMPLEX_UNIT_DPcenterTitleTv.setTextSize(TypedValue.COMPLEX_UNIT_SP,type.getDimension(R.styleable.CommonToolBar_centerBtnTextSize,13));