現(xiàn)在是在xml寫(xiě)一個(gè) toolbar,完事代碼里 setSupportActionBar(toolbar)
實(shí)際使用中應(yīng)該會(huì)發(fā)現(xiàn),xml里toolbar里如果你添加了一些view,這些view的最左邊并不是屏幕的左邊緣。
toolbar代碼如下,我給textview弄了個(gè)背景
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_collapseMode="pin"
android:paddingTop="24dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="title"
android:background="#44000000"
android:textColor="#fff" />
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical|right"
android:src="@mipmap/ic_launcher_round" />
</android.support.v7.widget.Toolbar>
如圖所示,可以看到陰影距離左邊是有一段距離的。

一路點(diǎn)進(jìn)去,可以發(fā)現(xiàn)在AppCompatDelegateImplV9類(lèi)里setSupportActionBar的具體實(shí)現(xiàn)
代碼如下
public void setSupportActionBar(Toolbar toolbar) {
//省略部分代碼
if (toolbar != null) {
final ToolbarActionBar tbab = new ToolbarActionBar(toolbar,
((Activity) mOriginalWindowCallback).getTitle(), mAppCompatWindowCallback);
mActionBar = tbab;
mWindow.setCallback(tbab.getWrappedWindowCallback());
} else {
mActionBar = null;
// Re-set the original window callback since we may have already set a Toolbar wrapper
mWindow.setCallback(mAppCompatWindowCallback);
}
invalidateOptionsMenu();
}
上邊代碼可以看出,new了一個(gè)ToolbarActionBar,繼續(xù)往下走
ToolbarActionBar(Toolbar toolbar, CharSequence title, Window.Callback windowCallback) {
mDecorToolbar = new ToolbarWidgetWrapper(toolbar, false);
mWindowCallback = new ToolbarCallbackWrapper(windowCallback);
mDecorToolbar.setWindowCallback(mWindowCallback);
toolbar.setOnMenuItemClickListener(mMenuClicker);
mDecorToolbar.setWindowTitle(title);
}
這里又new了一個(gè)ToolbarWidgetWrapper的東西
然后在構(gòu)造方法里發(fā)現(xiàn)了這樣的屬性,看名字很像
final TintTypedArray a = TintTypedArray.obtainStyledAttributes(toolbar.getContext(),
null, R.styleable.ActionBar, R.attr.actionBarStyle, 0);
final int contentInsetStart = a.getDimensionPixelOffset(
R.styleable.ActionBar_contentInsetStart, -1);
final int contentInsetEnd = a.getDimensionPixelOffset(
R.styleable.ActionBar_contentInsetEnd, -1);
if (contentInsetStart >= 0 || contentInsetEnd >= 0) {
mToolbar.setContentInsetsRelative(Math.max(contentInsetStart, 0),
Math.max(contentInsetEnd, 0));
}
試著在xml里contentInsetStart=0dp發(fā)現(xiàn)那個(gè)偏移沒(méi)了,那應(yīng)該就是了。
順道看下默認(rèn)值
我用的v7 27的版本,在design庫(kù)的res的values下找
<item name="contentInsetStart">@dimen/abc_action_bar_content_inset_material</item>
<item name="contentInsetStartWithNavigation">@dimen/abc_action_bar_content_inset_with_nav</item>
<item name="contentInsetEnd">@dimen/abc_action_bar_content_inset_material</item>
<dimen name="abc_action_bar_content_inset_material">16dp</dimen>
<dimen name="abc_action_bar_content_inset_with_nav">72dp</dimen>
我們還是回到toolbar看下,他的layout是咋處理這些控件的
protected void onLayout(boolean changed, int l, int t, int r, int b){
//省略。。這里只考慮默認(rèn)的布局從左到右,不考慮從右到左的布局
if (shouldLayout(mNavButtonView)) {
if (isRtl) {
right = layoutChildRight(mNavButtonView, right, collapsingMargins,
alignmentHeight);
} else {
//可以看到默認(rèn)是加在左邊的,而且是第一個(gè)添加的
left = layoutChildLeft(mNavButtonView, left, collapsingMargins,
alignmentHeight);
}
}
if (shouldLayout(mMenuView)) {
if (isRtl) {
left = layoutChildLeft(mMenuView, left, collapsingMargins,
alignmentHeight);
} else {
//可以看到menu里的東西默認(rèn)是添加在右邊的。
right = layoutChildRight(mMenuView, right, collapsingMargins,
alignmentHeight);
}
}
//這里處理了一下contentinsetleft和right
final int contentInsetLeft = getCurrentContentInsetLeft();
final int contentInsetRight = getCurrentContentInsetRight();
collapsingMargins[0] = Math.max(0, contentInsetLeft - left);
collapsingMargins[1] = Math.max(0, contentInsetRight - (width - paddingRight - right));
left = Math.max(left, contentInsetLeft);
//其實(shí)可以看到,如果上邊有back按鈕的話,那個(gè)left肯定很大了,這里其實(shí)contentinsetleft就沒(méi)啥用了。
right = Math.min(right, width - paddingRight - contentInsetRight);
//可以看到下邊的都是左邊的
if (shouldLayout(mExpandedActionView)) {
if (isRtl) {
} else {
left = layoutChildLeft(mExpandedActionView, left, collapsingMargins,
alignmentHeight);
}
}
//添加到左邊
if (shouldLayout(mLogoView)) {
if (isRtl) {
} else {
left = layoutChildLeft(mLogoView, left, collapsingMargins,
alignmentHeight);
}
}
//省略title。logo之類(lèi)的添加代碼
//最后才是添加我們?cè)趖oolbar里添加的view,也就是customview拉,比如開(kāi)頭的toolbar里添加的textview和imagview
// Get all remaining children sorted for layout. This is all prepared
// such that absolute layout direction can be used below.
addCustomViewsWithGravity(mTempViews, Gravity.LEFT);//循環(huán)找到gravity設(shè)置為left的view
final int leftViewsCount = mTempViews.size();
for (int i = 0; i < leftViewsCount; i++) {
//把這些重心在left的添加到左邊
left = layoutChildLeft(mTempViews.get(i), left, collapsingMargins,
alignmentHeight);
}
addCustomViewsWithGravity(mTempViews, Gravity.RIGHT);//循環(huán)找到gravity設(shè)置為right的view
final int rightViewsCount = mTempViews.size();
for (int i = 0; i < rightViewsCount; i++) {
//把這些重心為right的,從右邊開(kāi)始添加
right = layoutChildRight(mTempViews.get(i), right, collapsingMargins,
alignmentHeight);
}
// Centered views try to center with respect to the whole bar, but views pinned
// to the left or right can push the mass of centered views to one side or the other.
//最后添加其他沒(méi)有設(shè)置重心為left和right的,
addCustomViewsWithGravity(mTempViews, Gravity.CENTER_HORIZONTAL);
final int centerViewsWidth = getViewListMeasuredWidth(mTempViews, collapsingMargins);
final int parentCenter = paddingLeft + (width - paddingLeft - paddingRight) / 2;
final int halfCenterViewsWidth = centerViewsWidth / 2;
int centerLeft = parentCenter - halfCenterViewsWidth;
final int centerRight = centerLeft + centerViewsWidth;
if (centerLeft < left) {
centerLeft = left;
} else if (centerRight > right) {
centerLeft -= centerRight - right;
}
final int centerViewsCount = mTempViews.size();
for (int i = 0; i < centerViewsCount; i++) {
centerLeft = layoutChildLeft(mTempViews.get(i), centerLeft, collapsingMargins,
alignmentHeight);
}
}
總結(jié): 對(duì)于toolbar里邊的內(nèi)容是自定義的情況,如果不用系統(tǒng)的后退按鈕的話,去除那個(gè)默認(rèn)偏移量的話
在代碼里添加
app:contentInsetStart="0dp" 或者app:contentInsetLeft="0dp"
原本以為好了,結(jié)果發(fā)現(xiàn)6.0的機(jī)器上還是有個(gè)padding,而在8.0的機(jī)器是沒(méi)有padding的
好奇怪,如下圖

那就看下是不是有默認(rèn)的padding
``
public Toolbar(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, R.attr.toolbarStyle);
}
//去values下找
<item name="android:paddingLeft">@dimen/abc_action_bar_default_padding_start_material</item>
<item name="android:paddingRight">@dimen/abc_action_bar_default_padding_end_material</item>
完事在27的版本中找到的結(jié)果
<dimen name="abc_action_bar_default_padding_end_material">0dp</dimen>
<dimen name="abc_action_bar_default_padding_start_material">0dp</dimen>
現(xiàn)在有點(diǎn)暈啊,這些東西都是v7庫(kù)里的東西,不管運(yùn)行在6.0的手機(jī)還是8.0的手機(jī),這個(gè)庫(kù)應(yīng)該沒(méi)區(qū)別啊,按理數(shù)據(jù)是一樣的啊,太奇怪了。
后來(lái)突然醒悟過(guò)來(lái),我6.0的測(cè)試機(jī)是個(gè)平板,以我多年的經(jīng)驗(yàn),平板和手機(jī)有些數(shù)據(jù)是有區(qū)別的。。。
果不其然,我找到這個(gè)資源目錄values-sw600dp-v13
在下邊發(fā)現(xiàn)了,我擦,平板默認(rèn)是有個(gè)padding的。
<dimen name="abc_action_bar_default_padding_end_material">8dp</dimen>
<dimen name="abc_action_bar_default_padding_start_material">8dp</dimen>
這讓我想起以前重寫(xiě)Tablayout的時(shí)候也是被平板坑了,平板的tabgravity默認(rèn)是center的。
簡(jiǎn)單總結(jié)下
Toolbar 通過(guò) setSupportActionBar(toolbar) 被修飾成了actionbar。
Toolbar里的布局問(wèn)題,整體和線性布局差不多,一個(gè)挨一個(gè)的,從左加,從右加。
默認(rèn)的返回按鈕,title之類(lèi)的都在左側(cè),menu的東西都從右側(cè)添加
至于在toolbar里添加的其他view,首先按照view的gravity是left還是right添加,left的從左側(cè)添加,right的從右側(cè)添加。
其他的view是從left開(kāi)始添加的,
toolbar在平板上是有個(gè)默認(rèn)的8dp的paddingleft和right的。