Android的工具栏:标题和菜单溢出的5.0 aligment问题仅 [英] Android Toolbar: title and overflow menus aligment issue on 5.0 only

查看:154
本文介绍了Android的工具栏:标题和菜单溢出的5.0 aligment问题仅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Android应用程序下面的材料设计标准,并且为了保持兼容性我实现了一个自定义工具栏,如下所示:的 http://www.android4devs.com/2014/12/how-to-make-material-design-app.html与我的code为工具栏存在

I am developing an Android app following Material Design standards, and in order to maintain compatibility I implemented a custom Toolbar as shown here: http://www.android4devs.com/2014/12/how-to-make-material-design-app.html with my code for the toolbar being

<?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:theme="@style/AppToolbar"
        android:background="@color/primary"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:elevation="4dp" >

然后我推翻在值/风格的应用程序兼容性的风格和指定的小学和中学的颜色来调整动作条的布局。

I then overrode the AppCompat style in values/style and specified primary and secondary colors to adjust the ActionBar layout.

<resources>

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:colorPrimary">@color/primary</item>
    <item name="android:colorPrimaryDark">@color/primary_dark</item>
    <item name="android:textColor">@color/primary_text</item>
    <item name="android:textColorPrimary">@color/accent</item>
    <item name="android:textColorSecondary">@color/accent</item>
    <item name="android:actionMenuTextColor">@color/accent</item>
    <item name="android:itemTextAppearance">@style/menuAppearance</item>
    <item name="android:editTextColor">@color/primary_text</item>
    <item name="android:textColorHint">@color/secondary_text</item>
    <item name="android:colorControlHighlight">@color/accent</item>
    <item name="android:colorAccent">@color/accent</item>
</style>

<style name="menuAppearance" parent="@android:style/TextAppearance.Widget.IconMenu.Item">
    <item name="android:textColor">@color/accent</item>
</style>

</resources>

工具栏V21 /样式资源:

Toolbar v21/style resource:

<style name="AppToolbar" parent="android:Widget.Material.Toolbar">
    <item name="android:background">@color/primary</item>
    <item name="android:textColorPrimary">@color/accent</item>
</style>

我终于用setSupportActionBar(工具栏工具栏)方法来设置操作栏。活动标题是在清单中,而菜单项属性在各自的XML文件codeD。

I finally used setSupportActionBar(Toolbar toolbar) method to set the Action Bar. Activity titles are in the Manifest, while menu items attributes are coded in the respective xml files.

我想我的三星S3迷你应用(Android版本:4.1.2)和它的工作就好了。标题和菜单图标工具栏的垂直中心完全一致,并溢出菜单布局是确定的。
当华硕Zenfone2 ZE500CL移动安装棒棒堂(5.0),但是,我发现,标题和溢出菜单文本布局搞砸:他们被卡住了工具栏和溢出菜单列表顶部,而其他菜单项,如HomeIndicator始终-shown那些保持垂直居中到工具栏。

I tried my application on a Samsung s3 mini (Android version: 4.1.2) and it worked just fine. Titles and menu icon align perfectly at vertical center of the toolbar, and overflow menu layout is ok. When moving on an Asus Zenfone2 ZE500CL mounting Lollipop (5.0), however, I discovered that title and overflow menu texts layout was messed up: they are stuck up the Toolbar and overflow menu list top side, while other menu items such as HomeIndicator and always-shown ones remain vertically centered into the toolbar.

我花了互联网试图弄清楚这一点了一天,但所有问题都有关搞砸了布局,它可以通过指定工具栏适当的高度是固定的。很多人建议一个TextView添加到工具栏修复的称号,我想和它没有,但我的问题更大,因为溢出菜单项仍在顶部对齐。

I spent a day on the Internet trying to figure this out, but all questions were about messed up layouts which could be fixed by specifying a proper height for the toolbar. Lots of people suggested to add a TextView into the Toolbar to fix the title, I tried and it did, but my problem goes deeper, for Overflow menu items are still top aligned.

我不能直接添加屏幕截图,所以我张贴在谷歌驱动这一形象,希望它会使事情更清晰的 https://drive.google.com/file/d/0BzSgLr3IafUYUGhOS3BlYjY4UjA/view?usp=sharing
预先感谢您的帮助!

I can't add screenshots directly, so I'm posting this image from Google Drive, hoping it will make things clearer https://drive.google.com/file/d/0BzSgLr3IafUYUGhOS3BlYjY4UjA/view?usp=sharing Thanks in advance for your help!

推荐答案

我终于想出了解决我的问题;我分享它谁就面临着同样的问题。
我意识到只需要pre-棒棒糖设备工具栏,所以我创建的应用程序两种不同的主题:在风格上的资源V21版本,一个没有在style.xml资源的动作条,和一个与动作条。
到style.xml:

I eventually came up with the solution to my problem; I'm sharing it for whoever faces the same issue. I realized the toolbar is only needed for pre-Lollipop devices, so I created two different Themes for the application: one WITHOUT the ActionBar in style.xml resource, and one WITH the ActionBar in v21 version of the style resource. Into style.xml:

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    ...
</style>

进入V21 / style.xml:

Into v21/style.xml:

<style name="AppTheme" parent="Theme.AppCompat">
    ...
</style>

这样,只要pre-棒棒糖设备启动应用程序,它将推出动作条,少的版本。
需要改变在活动布局和类以及:为风格,我在原来的布局创建活动布局棒棒糖设备的V21版本,包括工具栏只:成布局/ activity_main.xml中

This way, whenever a pre-Lollipop device launches the app, it will launch the ActionBar-less version. Changes are needed in the Activity layout and class as well: as for style, I created a v21 version of the Activity layout for Lollipop devices and included the Toolbar in the original layout only: into layout/activity_main.xml

<RelativeLayout ...>

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />
...

最后,为onCreate方法插入此条件:

Finally, into OnCreate method inserted this conditional:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

为了填补只pre-棒棒糖设备空动作条。我想两个我的设备上的应用程序,它终于奏效。
请注意,对于XML资源我的意思是原始文件的副本,保存到RESOURCE_TYPE-v21文件夹中。

in order to fill up the empty ActionBar on pre-Lollipop devices only. I tried the application on both my devices and it finally worked. Note that for v21 version of an xml resource I mean a copy of the original file, saved into "resource_type"-v21 folder.

这篇关于Android的工具栏:标题和菜单溢出的5.0 aligment问题仅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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