当前位置: 移动技术网 > 移动技术>移动开发>Android > 解决Android V7后自定义Toolbar、ActionBar左侧有空白问题

解决Android V7后自定义Toolbar、ActionBar左侧有空白问题

2019年07月24日  | 移动技术网移动技术  | 我要评论

如图所示:

这里写图片描述

1.查看wiget.appcompat.toolbar的parent主题,如下所示:

<style name="base.widget.appcompat.toolbar" parent="android:widget"> 
 <item name="titletextappearance">@style/textappearance.widget.appcompat.toolbar.title</item> 
 <item name="subtitletextappearance">@style/textappearance.widget.appcompat.toolbar.subtitle</item> 
 <item name="android:minheight">?attr/actionbarsize</item> 
 <item name="titlemargins">4dp</item> 
 <item name="maxbuttonheight">56dp</item> 
 <item name="collapseicon">?attr/homeasupindicator</item> 
 <item name="collapsecontentdescription">@string/abc_toolbar_collapse_description</item> 
 <item name="contentinsetstart">16dp</item> 
</style>

其中,contentinsetstart这个属性就是引起自定义actionbar不能完全填充的原因。

解决方法:

在styles.xml中定义新的style继承自wiget.appcompat.toolbar如下:

<style name="clubtoolbar" parent="widget.appcompat.toolbar"> 
  <item name="contentinsetstart">0dp</item><!-- 设置该属性解决空白部分--> 
</style>

然后在你的style.xml中apptheme里重写toolbar属性:

<!-- base application theme. “noactionbar”隐藏原有的actionbar 设置toolbar -->
 <style name="apptheme" parent="theme.appcompat.light.noactionbar">
  //.....
  <item name="toolbarstyle">@style/clubtoolbar</item>
 </style>

2.也有另一个解决方法:

直接写到布局里,如下所示:

<android.support.v7.widget.toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/tool_bar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionbarsize"
    android:background="@color/colortheme"
    app:contentinsetstart="0.0dp"
    app:layout_scrollflags="enteralways|scroll"
    app:popuptheme="@style/themeoverlay.appcompat.light"
    app:theme="@style/toolbarstyle" />

app:contentinsetstart=”0.0dp”即可

以上所述是小编给大家介绍的解决android v7后自定义toolbar、actionbar左侧有空白问题,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网