11

I've just modified our code to use the new SupportActionBar provided in the v7-appcompat library but when running the code on a Jellybean phone (presumably the same problem exists for Honeycomb and Ice Cream Sandwich) the home button doesn't ever seem to be activated.

Calling getSupportActionBar().setHomeButtonEnabled(true); doesn't seem to do what it says but works for Gingerbread phones.

If I replace it with getActionBar().setHomeButtonEnabled(true) it does work.

The theme that I use for v11+ is as follows:

<style name="MyTheme" parent="@style/Theme.AppCompat">
    <item name="android:windowActionBar">true</item>
    <item name="android:windowNoTitle">false</item>
    <item name="android:listViewStyle">@style/MyListView</item>
    <item name="android:actionBarStyle">@style/MyActionBarStyle</item>
    <item name="android:windowSoftInputMode">stateAlwaysHidden</item>
    <item name="android:buttonStyle">@style/MyButton</item>
    <item name="android:radioButtonStyle">@style/MyRadioButtonStyle</item>
    <item name="android:windowContentOverlay">@drawable/ab_solid_dove_grey</item>
    <item name="android:windowTitleSize">@dimen/action_bar_height</item>
    <item name="android:selectableItemBackground">@drawable/sel_standard_item</item>
    <item name="android:windowBackground">@drawable/default_bg</item>
    <item name="android:actionMenuTextAppearance">@style/MyActionBarText</item>
    <item name="android:actionMenuTextColor">@color/gallery</item>
    <item name="android:tabWidgetStyle">@style/MyTabWidget</item>
</style>

And the action bar style v11+ is defined:

<style name="MyActionBarStyle" parent="android:style/Widget.Holo.ActionBar">
    <item name="android:displayOptions">useLogo|showHome|showCustom</item>
    <item name="displayOptions">useLogo|showHome|showCustom</item>
    <item name="android:actionBarSize">@dimen/action_bar_height</item>
    <item name="android:icon">@drawable/ic_launcher</item>
    <item name="android:background">@android:color/transparent</item> <!-- Remove blue line from bottom of action bar -->
</style>

Anyone know why the home button is not getting enabled when on an Android version that supports action bar correctly.

=== UPDATE === I've just looked at the source code for the appcompat library and I've noticed the following in ActionBarImplBase which looks wrong to me:

 setHomeButtonEnabled(abp.enableHomeButtonByDefault() || homeAsUp);

This means that the home button will only be enabled if the Android version is less than ICS or if I've enabled the up indicator? - which I don't want.

4

5 回答 5

21

这个对我有用:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_your_activity);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    // ... other stuff
}

@Override
public boolean onSupportNavigateUp(){
    finish();
    // or call onBackPressed()
    return true;
}

onSupportNavigateUp()当您使用中的后退按钮时调用该方法SupportActionBar

于 2015-01-20T19:58:09.863 回答
16

您是否尝试过使用所有这三个(也尝试交换getSupportActionbar())?

 getActionBar().setDisplayShowHomeEnabled(true);
 getActionBar().setHomeButtonEnabled(true);
 getActionBar().setDisplayHomeAsUpEnabled(true); 

您是否尝试过手动处理按钮?

@Override
public boolean onOptionsItemSelected(int featureId, MenuItem item) {
     int itemId = item.getItemId();
     if(itemId == android.R.id.home){
         // Do stuff
     }
     return true;
}
于 2013-10-10T10:15:08.880 回答
1

现在请试试这个。因为我能够像这样解决我自己的问题,尽管它是在 Sherlock 上。从您上面的样式中,我可以看到您对主题进行了一些自定义。在我的情况下,我对我的 Sherlock 主题进行了一些自定义,这就是在 android 4.0 及更高版本上给我带来问题的原因,我的主题失败了。所以我简单地添加了一段代码,告诉 android 在 android 4.0 及更高版本上运行时使用默认的 Sherlock 主题。所以我想这对你有用。你告诉 android 在不适合你的 android 版本上使用 v7-appcompat 库的默认主题。

代码如下:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
            this.setTheme(R.style.Theme_Sherlock);
        } else {
            this.setTheme(R.style.Theme_Sherlock_Light_DarkActionBar);

        }

在您的情况下,将主题编辑为 v7-appcompat 库。

如果对您有用,请标记为答案。我相信如果您正在使用它,可以从代码中自定义主题。

于 2013-10-11T14:27:07.013 回答
1

尝试为 Gingerbread 等 Android 设备使用 Sherlock 库,因为 android 操作栏仅支持 3.0 及更高版本,因此 sherlock 锁库为您提供向后兼容性。

http://actionbarsherlock.com/ --- 在这里下载库。

然后在您的代码中添加此行。

ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
         actionBar.setIcon(android.R.color.transparent);

        actionBar.setDisplayUseLogoEnabled(false);

这将帮助您在操作栏中添加返回主页键。如果您不希望它显示,它也会使您的图标不可见。但是,如果您希望您的应用程序图标显示在所有活动中,只需在下面评论此行

actionBar.setIcon(android.R.color.transparent);

于 2013-10-10T10:43:37.900 回答
0

在 API 级别 7 或更高级别上运行时,您可以通过为您的活动扩展ActionBarActivity类并将活动主题设置为 Theme.AppCompat 或类似主题,将 ActionBar 添加到活动中。

于 2013-10-11T04:25:29.927 回答