与自定义布局动作条不占用整个屏幕宽度在Android 4.4.2(奇巧) [英] ActionBar with custom layout does not occupy full screen width on Android 4.4.2 (KitKat)

查看:396
本文介绍了与自定义布局动作条不占用整个屏幕宽度在Android 4.4.2(奇巧)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示一个操作栏带有自定义布局。自定义布局有三个ImageViews,一个在中心,左边和功放其他两个;操作栏右端。必须有没有后退按钮或行动项目。

I want to display an Action Bar with a custom layout. The custom layout has three ImageViews, one in the centre and the other two on the left & right ends of the action bar. There must be no back button or action items.

下面是我用的是什么:

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);

在自定义布局充气正确,,但不占用屏幕的完整宽度。这发生在Android 4.4.2(奇巧)。在姜饼,上述安排工作正常。

The custom layout is inflated correctly, but does not occupy the full width of the screen. This happens on Android 4.4.2 (KitKat). On Gingerbread, the above arrangement works properly.

我试着在下面的讨论中建议的解决方法:

I've tried the recommended solutions in the following discussions:

  • <一个href="http://stackoverflow.com/questions/16026818/actionbar-custom-view-with-centered-imageview-action-items-on-sides/16029214#16029214">ActionBar - 与中心ImageView的自定义视图,两侧行动项目
  • <一个href="http://stackoverflow.com/questions/21375950/relativelayout-customview-doesnt-fill-tabs-width">RelativeLayout/customview不补片的宽度
  • ActionBar - custom view with centered ImageView, Action Items on sides
  • RelativeLayout/customview doesn't fill tab's width

但问题依然存在。必须有一个办法迫使布局,占据整个屏幕宽度。

However the problem remains. There has to be a way to force the layout to occupy the complete screen width.

我现在用的操作栏从V7 AppCompat库。有谁知道一个办法解决这个??

I am using the Action Bar from the V7 AppCompat library. Does anyone know a way around this ??

修改1:

下面是XML布局文件:

Here is the XML layout file:

<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>

下面是它看起来像姜饼(2.3.5):

Here is what it looks like on Gingerbread (2.3.5):

正如你所看到的,它正确地充气。下面是它的外观上奇巧(4.4.2):

As you can see, its inflating correctly. And here's how it looks on Kitkat (4.4.2):

您可以看到,有右侧有轻微间隙和布局不占用完整的宽度。

You can see that there's a slight gap on the right and the layout is not occupying the complete width.

我不相信有与布局(其在Android 2.3.5正确充气)的一个问题,但我可能是错的。告诉我,如果我疯了。

I dont believe there's a problem with the layout (its inflating correctly on Android 2.3.5), but I could be wrong. Tell me if I'm crazy.

编辑2:

由于 Doctoror硬盘和的这个帖子,我能得到我想要的结果。我知道它有点奇怪:为什么要使用一个操作栏,如果你不会有任何导航或行动项目,但是这是我需要为我的特殊问题。我有一个堆栈中的每个选项卡滑动片段的TabHost,顶部的LinearLayout(即操作栏)需要显示不同的片段时,以改变其外观。

Thanks to Doctoror Drive and this post, I was able to get the result I wanted. I know its a bit strange: why use an Action Bar if you are not going to have any navigation or action items, but this is what I need for my particular problem. I have a TabHost with a stack of sliding Fragments in each tab, and the top LinearLayout (i.e. the Action Bar) needs to change its appearance when different Fragments are displayed.

修改3:

下面是一些链接,进一步了解这一点:

Here are some more links for understanding this further:

  • https://groups.google.com/forum/#!msg/actionbarsherlock/i8JRUkBJjqk/ZzQV9xaM-lkJ
  • https://groups.google.com/forum/#!topic/android-developers/FGEi72thLzE

推荐答案

这不是一个缺口,这是一个菜单溢出按钮向右。如果你仔细观察,你可以看到三个点按钮。

It's not a gap, it's a menu overflow button to the right. If you look closely you can see three dots button.

您已经选择了错误的主题,因为你很难看到它。 使用 Theme.Compat.Light.DarkActionBar 如果你的动作条是黑暗的,你需要光的主题。

You've chosen the wrong theme because you can hardly see it. Use Theme.Compat.Light.DarkActionBar if your ActionBar is dark and you need the light theme.

在被示出的菜单按钮溢出没有物理菜单按钮装置(主要是新的)。

On devices that don't have physical menu button (mostly new ones) the menu overflow button is shown.

除非你关闭菜单您无法将其删除。

You can't remove it unless you disable menus.

您可以使用 splitActionBarWhenNarrow uiOptions 将拆分动作条,这样的菜单部分将在一个底部,但是这将是只为中等屏幕和可能不是你所需要的垂直方向的。

You can use splitActionBarWhenNarrow uiOptions which will split the ActionBar so that the menu part will be at the bottom one, but that will be only for vertical orientation of medium screens and probably not what you need.

看看的动作条模式DOC 和的菜单UI DOC

这篇关于与自定义布局动作条不占用整个屏幕宽度在Android 4.4.2(奇巧)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆