Android 自定义Toolbar/ActionBar视图左边有空白

本文讨论了在Android开发中使用自定义Toolbar时遇到的左侧留空白问题,并提供了通过调整contentInsetStart属性来解决该问题的方法。通过定义新的样式和在主题中重写toolbar属性,可以实现Toolbar的完全填充。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近自定义Toolbar之后,发现左侧不能完全填充,总是留一点空白,经过查看新发布的V7的支持包中的style发现了解决方法。 

 

查看Wiget.AppCompat.Toolbar的parent,如下:

?
1
2
3
4
5
6
7
8
9
10
< 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如下:
?
1
2
3
<style name= "ClubToolbar" parent= "Widget.AppCompat.Toolbar"
         <item name= "contentInsetStart" >0dp</item><!-- 设置该属性解决空白部分--> 
</style>

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

?
1
2
3
4
5
<!-- Base application theme. “NoActionBar”隐藏原有的Actionbar 设置Toolbar -->
     < style name = "AppTheme" parent = "Theme.AppCompat.Light.NoActionBar" >
         //.....
         < item name = "toolbarStyle" >@style/ClubToolbar</ item >
     </ style >


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值