ActionBar中的setHomeButtonEnabled与setDisplayHomeAsUpEnabled方法理解


在使用Actionbar或者用Toolbar的时候通常要设置setHomeButtonEnabled与setDisplayHomeAsUpEnabled


首先看一下源码对于setHomeButtonEnabled的介绍


/**
 * Enable or disable the "home" button in the corner of the action bar. (Note that this
 * is the application home/up affordance on the action bar, not the systemwide home
 * button.)
 *
 * 

This defaults to true for packages targeting < API 14. For packages targeting * API 14 or greater, the application should call this method to enable interaction * with the home/up affordance. * *

Setting the {@link #DISPLAY_HOME_AS_UP} display option will automatically enable * the home button. * * @param enabled true to enable the home button, false to disable the home button. */ public void setHomeButtonEnabled(boolean enabled) { }

看到源码中的介绍:启用或禁用actionbar右上角的“主页”按钮。(注意,这是操作栏,而不是全系统home键的应用程序.)-在API 14(Android 4.0)之前默认的是true,但是在API 14之后需要调用此方法来设置启用 右上角的按钮功能。该方法的作用:决定左上角的图标是否可以点击。

setDisplayHomeAsUpEnabled

/**
 * Set whether home should be displayed as an "up" affordance.
 * Set this to true if selecting "home" returns up by a single level in your UI
 * rather than back to the top level or front page.
 *
 * 

To set several display options at once, see the setDisplayOptions methods. * * @param showHomeAsUp true to show the user that selecting home will return one * level up rather than to the top level of the app. * * @see #setDisplayOptions(int) * @see #setDisplayOptions(int, int) */ public abstract void setDisplayHomeAsUpEnabled(boolean showHomeAsUp);

设置添加一个返回的图标,如果传入的参数是true,然后点击按钮的时候,会返回到此页面的前一页。

其他的两个方法

actionBar.setDisplayShowHomeEnabled(true)   //使左上角图标是否显示,如果设成false,则没有程序图标,仅仅就个标题,否则,显示应用程序图标,


actionBar.setDisplayShowCustomEnabled(true)  // 使自定义的普通View能在title栏显示,即actionBar.setCustomView能起作用。











你可能感兴趣的:(Android基础)