使用Actionbar Compat向上导航

时间:2013-11-21 11:02:00

标签: android android-actionbar android-actionbar-compat

按照开发者指南将应用程序图标设置为向上按钮,我完成了以下步骤:

在活动中:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.configuration);

    /**Enable app icon as Up button on ActionBar*/
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);

在清单中:

<activity 
    android:name="com.example.Configuration"
    android:label="@string/config_title"          
    android:parentActivityName="com.example.MainActivity" >
        <!-- Parent activity meta-data to support API level 7+ -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.MainActivity" />
</activity>

但仍然无法提升功能。

1 个答案:

答案 0 :(得分:1)

您必须在活动中写下此内容:

@Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.config_screen);
            actionBar.setDisplayHomeAsUpEnabled(false);
    }

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        try {
            switch (item.getItemId()) {

            case android.R.id.home:
                finish();
                Intent in= new Intent(Configuration.this,
                        MainACtivity.class);
                startActivity(in);
                break;
            }
        } catch (Exception ex) {
            Log.e("MainActivity - onOptionsItemSelected ", ex.getMessage());
            ex.printStackTrace();
        }

        return super.onOptionsItemSelected(item);
    }