简体   繁体   English

在行动酒吧的主页按钮不在棒棒糖工作

[英]Home button in action bar not working in Lollipop

I have a problem, cause I'm developing app for lollipop without support library v7 , so I need to use standard ActionBar . 我有一个问题,因为我正在为没有support library v7棒棒糖开发app,所以我需要使用标准的ActionBar Previously I used setHomeButtonEnabled in multiple activities with actionBar . 以前我用setHomeButtonEnabled与多个活动actionBar

When I switched app theme to material everything is working fine, except setHomeButtonEnabled method. 当我将app主题切换为材质时,除了setHomeButtonEnabled方法外,一切正常。

Title is not clickable. 标题不可点击。

Help! 救命!

For some reason, someone at Google decided that these should become no-op's. 出于某种原因,Google的某个人认为这些应该成为无操作者。 This can be seen below. 这可以在下面看到。

ToolbarWidgetWrapper.java ToolbarWidgetWrapper.java

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

ToolbarActionBar.java ToolbarActionBar.java

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

It seem's as though ToolbarWidgetWrapper which is used to map the Toolbar to ActionBar differences treats the Nav Icon in the Toolbar as the 'Home As Up Indicator'. 似乎用于将工具栏映射到ActionBar差异的ToolbarWidgetWrapper将工具栏中的导航图标视为“Home As Up Indicator”。 So only that part is clickable. 所以只有那部分是可点击的。 The new Logo you can set on Toolbar can never be clickable. 您可以在工具栏上设置的新徽标永远无法点击。 They don't even expose the view, its private rolls-eyes . 他们甚至没有暴露视图,它的private 卷眼

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" . 因此,第一个解决办法是,如果你不使用“家庭高达”功能,或者您愿意为你绘制了与您的家庭可绘制结合,那么你可以简单地设置android:homeAsUpIndicator属性在您的android:actionBarStyle沿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>

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. 最好选择主题选项。

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. 哪个使用android:navigationButtonStyle但猜猜是什么......他们没有公开这个。 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. 我相信这是一个绝对的错误,但这一切都开始变得像我们在ActionBar中首先遇到的原始问题。

So lets work around, the work around. 所以,让我们来解决,解决问题。 Ok let's try just setting the android:navigationIcon in the android:toolbarStyle . 好吧,让我们尝试只需设置android:navigationIconandroid:toolbarStyle Nope. 不。 Ok lets try all other possible variations you can think of including using android:actionBarStyle and android:actionBarTheme . 好吧,让我们尝试所有其他可能的变化你可以想到包括使用android:actionBarStyleandroid:actionBarTheme Nope. 不。 This I expect is because the navigationIcon is being overridden some place in the default ActionBar/Activity/Toolbar mess. 我希望这是因为在默认的ActionBar / Activity / Toolbar混乱中某些地方覆盖了navigationIcon。 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. 这意味着如果您已经使用自定义导航,则必须将导航与此导航结合使用。

values-v21/styles.xml 值-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>

layout-v21/nav_layout.xml 布局-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"
    />

In your Activity/BaseActivity class add the following. 在您的Activity / BaseActivity类中添加以下内容。 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);
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM