ActionBarSherlock左上角图标无法向上导航

时间:2013-02-10 15:20:29

标签: android android-actionbar actionbarsherlock

我正在使用ActionBarSherlock为Android HoneyComb的前/后版本实现ActionBar。 我的问题是,当我点击Android 4.0.4上的左上角图标时,它没有响应。 以下是我到目前为止所做的事情:

1)在所有样式文件夹“values / styles.xml”,“values-v11 / styles.xml”& “值-V14 / styles.xml”;我做了以下

<style name="ActivityTheme" parent="@style/AppTheme">
        <item name="actionBarStyle">@style/ActivityActionBarStyle</item>
        <item name="android:actionBarStyle">@style/ActivityActionBarStyle</item>
    </style>
<style name="ActivityActionBarStyle" parent="ommaralrd_transparent_ActionBar">
        <item name="displayOptions">showHome|showTitle|homeAsUp</item>
        <item name="android:displayOptions">showHome|showTitle|homeAsUp</item>
    </style>

在任何应用程序活动中(除了主页活动,因为它不应该有向上箭头),我已完成以下操作:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inner);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB){
            getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        }
.....rest of my code ...
}


@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent;
        switch (item.getItemId()) {
        case android.R.id.home:

            /*app icon in action bar clicked; go home*/
            intent = new Intent(this, MainActivity.class);

            /* With this flag, if the activity you're starting already exists in the current task, 
             * then all activities on top of it are destroyed and it is brought to the front.
             * you should usually not create a new instance of the home activity. Otherwise, 
             * you might end up with a long stack of activities in the current task with multiple 
             * instances of the home activity.*/
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            break;
        default:
            return super.onOptionsItemSelected(item);
        }
        return true;
    }

在Manifest文件中,我确保我已将相应的样式应用于所有活动(主要活动除外,因为它不应该有向上箭头)

<activity
            android:name="com.andrid.example.TestActivity"
            android:label="@string/app_name"
            android:theme="@style/ActivityTheme" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.andrid.example.MainActivity" />
        </activity>

所以现在当我在Pre-HoneyComb版本上测试应用程序时,永远不会显示向上箭头是正确的,因为如果点击图标导航,ABS根本无法响应。 但是当我在模拟器上的4.1版本的Post-HoneyComb版本上尝试应用程序时,会显示向上箭头,当我点击它时,它按预期工作并正常导航。 我的问题是,当我在Android 4.0.4模拟器上尝试应用程序时,向上箭头显示为预期但当我点击它时,它什么都不做

1 个答案:

答案 0 :(得分:0)

我找到了修复: 简单地说,我应该避免使用xml属性将Home Home设置为Up:homeAsUp,但我应该明确地使用setDisplayHomeAsUpEnabled(true),如下所示:

在所有样式文件夹“values / styles.xml”,“values-v11 / styles.xml”&amp; “值-V14 / styles.xml”;我做了以下

<style name="ActivityTheme" parent="@style/AppTheme">
        <item name="actionBarStyle">@style/ActivityActionBarStyle</item>
        <item name="android:actionBarStyle">@style/ActivityActionBarStyle</item>
    </style>
<style name="ActivityActionBarStyle" parent="ommaralrd_transparent_ActionBar">
        <item name="displayOptions">showHome|showTitle</item>
        <item name="android:displayOptions">showHome|showTitle</item>
    </style>
每个活动中的

(主要活动除外):

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
相关问题