在ActionBarSherlock的左上角用图标导航左上角、图标、ActionBarSherlock

2023-09-12 02:41:11 作者:心病无药医

使用开发者指南中找到 此处 ,我试图让我的图标导航回到我的主屏幕。我现在有一个按钮,这样做,并有复制和粘贴的code。在 onOptionsItemSelected()方法。但是攻从来没有做任何事情的图标。这是在动作条和ActionBarSherlock区别吗?

Using the developers guide found here, I am trying to make my icon navigate back to my home screen. I currently have a button which does this, and have copy and pasted the code in the onOptionsItemSelected() method. However tapping the icon never does anything. Is this a difference in ActionBar and ActionBarSherlock?

这是作为一个例子给出了code:

This is the code given as an example:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
    case android.R.id.home:
        // app icon in action bar clicked; go home
        Intent intent = new Intent(this, HomeActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        return true;
    default:
        return super.onOptionsItemSelected(item);
}
}

这是我现在用的是code:

This is the code that I am using:

public boolean onOptionsItemSelected( MenuItem item ) {
    switch( item.getItemId() ) {
    case R.id.mainTopBluetoothState:
        Toast.makeText( this, "BluetoothState", Toast.LENGTH_SHORT ).show();
        return true;
    case R.id.mainTopAppState:
        Toast.makeText( this,  "BluetoothState",  Toast.LENGTH_SHORT ).show();
        return true;
    case android.R.id.home:
        Log.i( "In Home", "In Home" );
        killToasts();
        dispatchKeyEvent(new KeyEvent( KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK ));
        finish();
        return true;
    }
    return super.onOptionsItemSelected( item );
}

当我点击图标,没有任何反应。在code的登录呼叫没有以往任何时候都显示我的 LogCat中或者

When I tap the icon, nothing happens. The Log call in the code isn't ever shown in my LogCat either.

推荐答案

你可能不会使ABS活动标志,点击。在的onCreate加入这个()

You are probably not enabling the ABS Activity logo to be clickable. Add this in onCreate()

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

另外,如果你还没有这样做的话,读Implementing先祖导航 让您浏览最多的正确的(忽略其使用的(),他们没有采用ABS getActionBar ,这就是实际的Andr​​oid API操作栏的方法)。

Also, if you haven't done so already, read Implementing Ancestral Navigation so you navigate up properly (ignore their use of getActionBar(), they're not using ABS, and that's the actual Android API Action Bar method).