Toolbar 自定义布局去除左右两边间距
在工作中遇到了toolbar 嵌套布局发现左右边距不一样大 ,所以发现了并加以修改
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/holo_blue_bright">
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_red_dark" />
</android.support.v7.widget.Toolbar>
经过查阅
app:contentInsetStart=”0dp”
只不过是间距变窄了一些。经过测试,发现还要设置 toolbar 的 paddingLeft 和 paddingRight 值为 0,当然,直接设置 padding 为 0 也是可以的。加上之后终于没有间距了:
如果在代码里设置了显示返回箭头,即
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
则需要给布局添加属性设置:
app:contentInsetStartWithNavigation=”0dp”
可以看到,返回箭头占的位置变窄了。
最后,完整布局是这样的:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/holo_blue_bright"
android:padding="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp">
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_red_dark" />
</android.support.v7.widget.Toolbar>
clone:https://blog.csdn.net/musiclife123/article/details/80417825?utm_source=blogxgwz1