在操作栏中的Home键不灵的棒棒糖 [英] Home button in action bar not working in Lollipop

查看:155
本文介绍了在操作栏中的Home键不灵的棒棒糖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,因为我开发的应用程序的棒棒糖无支持库V7 ,所以我需要使用标准的动作条。 previously我用 setHomeButtonEnabled 在多个活动动作条

I have a problem, cause I'm developing app for lollipop without support library v7, so I need to use standard ActionBar. Previously I used setHomeButtonEnabled in multiple activities with actionBar.

当我切换应用程序的主题,材料一切正常,除了setHomeButtonEnabled方法。

When I switched app theme to material everything is working fine, except setHomeButtonEnabled method.

标题无法点击。

帮助!

推荐答案

出于某种原因,有人在谷歌决定,这些应该成为无操作的。这在下面可以看到。

For some reason, someone at Google decided that these should become no-op's. This can be seen below.

<一个href="https://github.com/android/platform_frameworks_base/blob/master/core/java/com/android/internal/widget/ToolbarWidgetWrapper.java#L471">ToolbarWidgetWrapper.java

@Override
public void setHomeButtonEnabled(boolean enable) {
    // Ignore
}

<一个href="https://github.com/android/platform_frameworks_base/blob/master/core/java/com/android/internal/app/ToolbarActionBar.java#L129">ToolbarActionBar.java

ToolbarActionBar.java

@Override
public void setHomeButtonEnabled(boolean enabled) {
    // If the nav button on a Toolbar is present, it's enabled. No-op.
}

这似乎的好像ToolbarWidgetWrapper这是用来映射工具栏动作条差别对待导航图标工具栏中的回家向上指示器。因此,只有这一部分是可以点击的。新的标识,你可以在工具栏设置不能点击。他们甚至不露出观点,其私人卷眼的。

因此​​,第一个解决办法是,如果你不使用'回家向上功能,或者你愿意你了绘制与您的家庭可绘制相结合,那么你可以简单地设置安卓homeAsUpIndicator Android的属性:actionBarStyle 以及机器人:displayOptions =homeAsUp

So the first work around is if you don't use the 'Home As Up' feature or you are willing to combine your up drawable with your home drawable then you could simply set the android:homeAsUpIndicator attribute in your android:actionBarStyle along with android:displayOptions="homeAsUp".

<style name="AppBaseTheme" parent="@android:style/Theme.Material.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/ActionBar</item>
</style>

<style name="ActionBar" parent="@android:style/Widget.Material.Light.ActionBar.Solid">
    <item name="android:background">@color/action_bar_bg</item>
    <item name="android:displayOptions">homeAsUp</item>
    <item name="android:homeAsUpIndicator">@drawable/app_logo</item>
</style>

显然,这同样的事情可以用code来完成。

Obviously this same thing can be done with code.

getActionBar().setHomeAsUpIndicator(R.drawable.app_logo);
getActionBar().setDisplayHomeAsUpEnabled(true);

但是,这意味着它不会出现,而该活动实际上是加载。所以最好去与主题化选项。

But this means it will not appear whilst the activity is actually loading. So best to go with the theming option.

第三选项,这将是最好的就是样式导航键为新的工具栏视图。它采用安卓navigationButtonStyle ,但你猜怎么着......他们没有一个公开的。我相信这是一个geninue错误但这一切开始感觉很像原来的问题,我们都在第一时间曾与动作条。

The 3rd option which would be best is just to style the navigation button for the new Toolbar view. Which uses android:navigationButtonStyle but guess what.. they didn't make that one public. I believe this is a geninue mistake but this is all starting to feel much like the original problems we all had with ActionBar in the first place.

因此​​,让周围的工作,工作的周围。好吧,让我们尝试只设置了安卓navigationIcon 安卓toolbarStyle 。都能跟得上。好让我们尝试其他所有可能的变化,你可以想到的,包括使用的android:actionBarStyle 安卓actionBarTheme 。都能跟得上。这是我想到的是,因为navigationIcon被覆盖在默认的动作条/活动/工具栏乱七八糟的一些地方。我不能我懒得去查找地点和方式解决它。

So lets work around, the work around. Ok let's try just setting the android:navigationIcon in the android:toolbarStyle. Nope. Ok lets try all other possible variations you can think of including using android:actionBarStyle and android:actionBarTheme. Nope. This I expect is because the navigationIcon is being overridden some place in the default ActionBar/Activity/Toolbar mess. I can't me bothered to locate where and work a way around it.

好了,终于可以让选择最灵活的选择。删除默认主页按钮和使用我们自己通过实施旧的自定义导航功能。这也意味着,如果你已经使用自定义导航你必须将你的资产净值与这一个结合。

Ok, so finally lets choose the most flexible option. Drop the default Home Button and use our own by implementing the older custom navigation feature. This does mean if you already use a custom nav you'll have to combine your nav with this one.

值-V21 / styles.xml

values-v21/styles.xml

<style name="AppBaseTheme" parent="@android:style/Theme.Material.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/ActionBar</item>
</style>

<style name="ActionBar" parent="@android:style/Widget.Material.Light.ActionBar.Solid">
    <item name="android:background">@color/action_bar_bg</item>
    <item name="android:displayOptions">showCustom</item>
    <item name="android:customNavigationLayout">@layout/nav_layout</item>
</style>

布局-V21 / nav_layout.xml

layout-v21/nav_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/home"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="start|center_vertical"
    android:layout_marginEnd="8dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    android:adjustViewBounds="true"
    android:background="?android:attr/actionBarItemBackground"
    android:contentDescription="@string/ab_home_description"
    android:src="@drawable/app_logo"
    android:scaleType="fitCenter"
    />

在你的活动/ BaseActivity类中添加以下内容。这将假的home键。

In your Activity/BaseActivity class add the following. And this will fake the home button.

@Override
public boolean onPrepareOptionsMenu(final Menu menu)
{
    prepareOptionsMenuLollipop(menu);
    return super.onPrepareOptionsMenu(menu);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void prepareOptionsMenuLollipop(Menu menu)
{
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
        final MenuItem homeItem = menu.add(Menu.NONE, android.R.id.home, 
            Menu.NONE, R.string.ab_home_description);
        homeItem.setVisible(false);
        View homeView = getActionBar().getCustomView().findViewById(android.R.id.home);
        homeView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v)
            {
                getWindow().getCallback().onMenuItemSelected(Window.FEATURE_OPTIONS_PANEL, homeItem);
            }
        });
    }
}

这篇关于在操作栏中的Home键不灵的棒棒糖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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