Toolbar在项目中的运用比较普遍,今天来学习一下它的基本用法:今天就仿微信写一个大概样式
第一步: 在AndroidManifest.xml文件中更改application标签的theme属性的值为
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".ToolBarActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
xml文件布局
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#333333"
app:contentInsetStart="0dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:text="微信"
android:textColor="#fff"
android:textSize="20sp" />
<ImageView
android:id="@+id/img"
android:src="@mipmap/tt"
android:layout_marginRight="30dp"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_width="20dp"
android:layout_height="20dp" />
<ImageView
android:layout_marginRight="40dp"
android:src="@mipmap/ttt"
android:layout_toLeftOf="@+id/img"
android:layout_centerInParent="true"
android:layout_width="20dp"
android:layout_height="20dp" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
效果图:(没微信的好看)
需要注意的是:toolbar 是v7包中的控件 ,在使用toolbar的时候需要先隐藏actionbar ,toolbar中的控件默认和左边框有一定的距离 如果想要自定义距离则利用 toolbar的属性
app:contentInsetStart="0dp"
通过此属性就能让控件在toolbar的最左侧进行显示 然后根据实际需求定制自己想要的间距.