2012-11-21 20 views
32

我想在操作欄中啓用主頁按鈕。我使用此代碼:setDisplayHomeAsUpEnabled和setHomeButtonEnabled有什麼區別?

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 
      actionbar.setHomeButtonEnabled(true); 
      actionbar.setDisplayHomeAsUpEnabled(true); 
} 

在此我使用setHomeButtonEnabledsetDisplayHomeAsUpEnabled把背部標記在動作條圖標。如果我只使用setDisplayHomeAsUpEnabled那麼它也會起作用嗎?是否需要將setHomeButtonEnabled設置爲true?

兩者有什麼區別?

回答

65

對於你想要做的,actionBar.setDisplayHomeAsUpEnabled(true)就足夠了。

對於區別:
actionBar.setHomeButtonEnabled(true)只會使圖標可點擊,圖標背景的顏色作爲點擊的反饋。
actionBar.setDisplayHomeAsUpEnabled(true)將使圖標可點擊並在圖標左側添加<

+0

當只有'setHomeButtonEnabled(真)'設置,將它仍然叫'onOptionsItemSelected'? –

+0

我這麼認爲,但我沒有檢查。 – yDelouis

+5

爲了記錄,我檢查並且'setHomeButtonEnabled(true)'**仍然調用'onOptionsItemSelected',並且可以以'item.getItemId()== android.R.id.home'的相同方式捕獲。所以唯一的區別是''圖標。 –

3

由於Android的說:

- setDisplayShowHomeEnabled(boolean showHome) 
    // Set whether to include the application home affordance in the action bar. 
    // (and put a back mark at icon in ActionBar for "up" navigation) 

-setHomeButtonEnabled(boolean enabled) 
    // Enable or disable the "home" button in the corner of the action bar. 
    // (clickable or not) 

它應該是很清楚的,我認爲