setDisplayHomeAsUpEnabled和setHomeButtonEnabled有什么区别?

Jai*_*dra 39 android android-actionbar

我想在操作栏中启用主页按钮.我正在使用此代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            actionbar.setHomeButtonEnabled(true);
            actionbar.setDisplayHomeAsUpEnabled(true);
}
Run Code Online (Sandbox Code Playgroud)

在这里,我正在使用setHomeButtonEnabledsetDisplayHomeAsUpEnabled在ActionBar中的图标上添加一个背面标记.如果我只使用setDisplayHomeAsUpEnabled那么它也会起作用吗?是否需要设置setHomeButtonEnabled为true?

两者有什么区别?

yDe*_*uis 77

对于你想做的事,actionBar.setDisplayHomeAsUpEnabled(true)就足够了.

区别在于:
actionBar.setHomeButtonEnabled(true)只会使图标可点击,图标背景的颜色作为点击的反馈.
actionBar.setDisplayHomeAsUpEnabled(true)将使图标可点击并添加<图标左侧的图标.

  • 为了记录,我检查并且`setHomeButtonEnabled(true)`**确实**仍然调用`onOptionsItemSelected`并且可以以`item.getItemId()== android.R.id.home`的相同方式捕获.所以唯一的区别是`<`图标. (5认同)

kin*_*mer 5

正如安卓所说:

- 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)
Run Code Online (Sandbox Code Playgroud)

我想应该很清楚