Toolbar 相對於 ActionBar的強大之處在於,ToolBar有更強大的自定義效果。因爲ToolBar本身就是一個ViewGroup,可以往Toolbar中放入各種組件。
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="@+id/toolbar"
android:elevation="5dp"
android:background="#339999">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F00">
<Button
android:id="@+id/toolbar_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Button"/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/ic_launcher"
android:layout_centerInParent="true" />
<TextView
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="鄧紫棋" />
</RelativeLayout>
可以根據自己的需求,設置各種效果。但是,左邊的邊距一直去不了,如果知道的朋友,請給我留言,謝謝!