带有自定义布局的ActionBar在Android 4.4.2(KitKat)上不占用全屏宽度

Y.S*_*Y.S 5 android android-layout android-actionbar

我想显示一个带有自定义布局的Action Bar.自定义布局有三个ImageView,一个位于中心,另外两个位于操作栏的左右两端.必须没有后退按钮或操作项.

这是我用过的:

android.support.v7.app.ActionBar actionBar = getSupportActionBar();

actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);

ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
View actionBarView = LayoutInflater.from(this).inflate(resource, null); 
actionBar.setCustomView(actionBarView, lp);
Run Code Online (Sandbox Code Playgroud)

自定义布局正确充气,但不占据屏幕的整个宽度.这发生在Android 4.4.2(KitKat)上.在Gingerbread上,上述安排正常.

我在以下讨论中尝试了推荐的解决方案:

但问题仍然存在.必须有一种方法来强制布局占据整个屏幕宽度.

我正在使用V7 AppCompat库中的Action Bar.有没有人知道这个方法?

编辑1:

这是XML布局文件:

<RelativeLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="fill_horizontal"
            android:orientation="horizontal">

                    <ImageView
                         android:id="@+id/left_icon"
                         android:background="@drawable/leftbutton"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:layout_marginLeft="10dp"
                         android:layout_alignParentLeft="true"
                         android:layout_centerVertical="true"
                         android:adjustViewBounds="true"
                         android:scaleType="centerInside"
                         android:clickable="true"
                         android:onClick="leftButtonPressed"
                         android:hapticFeedbackEnabled="true"
                    />

                    <ImageView
                         android:id="@+id/center_icon"
                         android:background="@drawable/centertitle"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:layout_centerInParent="true"
                         android:adjustViewBounds="true"
                         android:scaleType="centerInside"
                    />

                    <ImageView
                         android:id="@+id/right_icon"
                         android:background="@drawable/rightbutton"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:layout_marginRight="10dp"
                         android:layout_alignParentRight="true"
                         android:layout_centerVertical="true"
                         android:adjustViewBounds="true"
                         android:scaleType="centerInside"
                         android:clickable="true"
                         android:onClick="rightButtonPressed"
                         android:hapticFeedbackEnabled="true"
                    />
                </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

这是Gingerbread(2.3.5)的样子:

Gingerbread Action Bar http://i62.tinypic.com/4utfu0.png

如你所见,它正确膨胀.以下是它在Kitkat上的表现(4.4.2):

Kitkat Action Bar http://i59.tinypic.com/vosfex.png

您可以看到右侧有一个小间隙,布局不占用整个宽度.

我不相信布局有问题(它在Android 2.3.5上正确膨胀),但我可能是错的.告诉我,如果我疯了.

编辑2:

感谢Doctoror Drive这篇文章,我得到了我想要的结果.我知道它有点奇怪:如果你不打算使用任何导航或动作项目,为什么要使用Action Bar,但这是我对我的特定问题所需要的.我有一个TabHost,每个标签中都有一堆滑动碎片,当显示不同的碎片时,顶部的LinearLayout(即操作栏)需要更改其外观.

编辑3:

以下是一些进一步理解这一点的链接:

Yar*_*lyk 1

这不是一个间隙,而是右侧的菜单溢出按钮。如果仔细观察,您可以看到三个点按钮。

您选择了错误的主题,因为您几乎看不到它。Theme.Compat.Light.DarkActionBar如果您的 ActionBar 是深色的并且您需要浅色主题,请使用。

在没有物理菜单按钮的设备(大多数是新设备)上,会显示菜单溢出按钮。

除非禁用菜单,否则无法将其删除。

您可以使用splitActionBarWhenNarrow uiOptions它将 ActionBar 分割,以便菜单部分位于底部,但这仅适用于中等屏幕的垂直方向,可能不是您所需要的。

查看ActionBar Patterns 文档Menus UI 文档