一、setHomeAsUpIndicator()
getSupportActionBar().setHomeAsUpIndicator(R.drawable.btn_back);
跟踪源码(ToolbarWidgetWrapper.java)最终发现调用了这个方法
private void ensureNavButtonView() {
if (mNavButtonView == null) {
mNavButtonView = new ImageButton(getContext(), null,
R.attr.toolbarNavigationButtonStyle);
final LayoutParams lp = generateDefaultLayoutParams();
lp.gravity = GravityCompat.START | (mButtonGravity & Gravity.VERTICAL_GRAVITY_MASK);
mNavButtonView.setLayoutParams(lp);
}
}
toolbarNavigationButtonStyle 找到样式找到
<item name="toolbarNavigationButtonStyle">@style/Widget.AppCompat.Toolbar.Button.Navigation</item>
最终样式
<style name="Base.Widget.AppCompat.Toolbar.Button.Navigation" parent="android:Widget"> <item name="android:background">?attr/controlBackground</item> <item name="android:minWidth">56dp</item> <item name="android:scaleType">center</item></style>
可见是因为android:minWidth 在做怪
解决:
<item name="toolbarNavigationButtonStyle">@style/MyToolbarNavigationButtonStyle</item>
</style>
<style name="MyToolbarNavigationButtonStyle" parent="Widget.AppCompat.Toolbar.Button.Navigation">
<item name="android:minWidth">0dp</item>
<item name="android:paddingLeft">@dimen/View_margin_10</item>
</style>
二、setlogo()
public void setLogo(Drawable drawable) {
if (drawable != null) {
ensureLogoView();
if (!isChildOrHidden(mLogoView)) {
addSystemView(mLogoView, true);
}
} else if (mLogoView != null && isChildOrHidden(mLogoView)) {
removeView(mLogoView);
mHiddenViews.remove(mLogoView);
}
if (mLogoView != null) {
mLogoView.setImageDrawable(drawable);
}
}
private void ensureLogoView() {
if (mLogoView == null) {
mLogoView = new ImageView(getContext());
}
}