Toolbar自定义布局
生活随笔
收集整理的這篇文章主要介紹了
Toolbar自定义布局
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Toolbar如何使用想必大家清楚地很,實際開發中標題欄的樣式各色各樣,因此其基本樣式便不能滿足我們的需求,這就需要我們自定義布局。打開ToolBar源碼我們發現它繼承ViewGroup,這就表示我們可以把它當做一個存放控件的容器。
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="標題"
android:textSize="16sp"
android:textColor="@color/white"
/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
結果:左邊出現一片空白
查找源碼發現:
final int contentInsetStart =
a.getDimensionPixelOffset(R.styleable.Toolbar_contentInsetStart,
RtlSpacingHelper.UNDEFINED);
final int contentInsetEnd =
a.getDimensionPixelOffset(R.styleable.Toolbar_contentInsetEnd,
RtlSpacingHelper.UNDEFINED);
final int contentInsetLeft =
a.getDimensionPixelSize(R.styleable.Toolbar_contentInsetLeft, 0);
final int contentInsetRight =
a.getDimensionPixelSize(R.styleable.Toolbar_contentInsetRight, 0);
mContentInsets.setAbsolute(contentInsetLeft, contentInsetRight);
if (contentInsetStart != RtlSpacingHelper.UNDEFINED ||
contentInsetEnd != RtlSpacingHelper.UNDEFINED) {
mContentInsets.setRelative(contentInsetStart, contentInsetEnd);
}
是這個contentInsetStart導致了左邊的留白,我們只要將左邊距置為0即可。
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="標題"
android:textSize="16sp"
android:textColor="@color/white"
/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
這樣Toobar的樣式就可以任意由我們修改了
總結
以上是生活随笔為你收集整理的Toolbar自定义布局的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ Boost库简介
- 下一篇: BigPipe学习研究