Haw使ActionBar在大屏幕上全宽(平板电脑)



在我的应用程序中,我使用AppCompat v7使用ActionBar。我注意到,在设备上,ActionBar的左侧和右侧都有类似的填充(空白)。我已经用这个解决方案解决了这个问题:

<resources>
<!-- the theme applied to the application or activity -->
<style name="AppTheme"
    parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Support library compatibility -->
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
    parent="Widget.AppCompat.Toolbar">
    <!-- Support library compatibility -->
    <item name="contentInsetStart">0dp</item>
    <!--<item name="background">@android:color/transparent</item>-->
</style>

在我的手机(5英寸)上一切看起来都很好,但在平板电脑等大屏幕上,问题仍然存在。。。为什么?

我认为使用工具栏是答案,但如果有人不想重建应用程序,这里有(不太优雅的)解决方案:

用途:

ab.setCustomView(R.layout.actionbar_main);
Toolbar parent =(Toolbar) ab.getCustomView().getParent();
parent.setContentInsetsAbsolute(0,0);
parent.setBackgroundColor(Color.parseColor("#00A0D1"));

而不是使用

<item name="contentInsetStart">0dp</item>

在style.xml 中

"奇怪"的填充仍然存在,但颜色与ActionBar相同。现在,即使在大屏幕上,一切看起来都很好。

最新更新