一趟妥、setHomeAsUpIndicator()
getSupportActionBar().setHomeAsUpIndicator(R.drawable.btn_back);
跟蹤源碼(ToolbarWidgetWrapper.java)最終發(fā)現(xiàn)調(diào)用了這個(gè)方法
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>
可見是因?yàn)閍ndroid: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());
}
}