最近在完善一個(gè)issue,其中需要給新創(chuàng)建的頁(yè)面添加返回按鈕,代碼非常簡(jiǎn)單:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);//給左上角添加一個(gè)返回的箭頭圖標(biāo)
getSupportActionBar().setHomeButtonEnabled(true);//設(shè)置圖標(biāo)可以點(diǎn)擊
getSupportActionBar().setDisplayShowHomeEnabled(true);//使圖標(biāo)可以顯示
點(diǎn)擊還是沒(méi)反應(yīng),在網(wǎng)上查找,發(fā)現(xiàn)缺少如下代碼
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home) //應(yīng)用程序圖標(biāo)的id
{
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
終于,返回功能正常使用。