3 回答

TA貢獻(xiàn)1921條經(jīng)驗(yàn) 獲得超9個(gè)贊
如果我假設(shè)您在布局中使用 android.support.v4.widget.DrawerLayout,那么這種方法可能適合您;我只在 API 21 上進(jìn)行了測試,但考慮到它主要使用支持庫,它應(yīng)該可以在較低或較高的目標(biāo)上工作(著名的最后一句話)。
導(dǎo)入 android.support.v7.app.ActionBarDrawerToggle 導(dǎo)入 android.support.v4.widget.DrawerLayout
ActionBarDrawerToggle mDrawerToggle;
DrawerLayout drawerLayout;
private boolean mToolBarNavigationListenerIsRegistered = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
// Get DrawerLayout ref from layout
drawerLayout = (DrawerLayout)findViewById(R.id.drawer);
// Initialize ActionBarDrawerToggle, which will control toggle of hamburger.
// You set the values of R.string.open and R.string.close accordingly.
// Also, you can implement drawer toggle listener if you want.
mDrawerToggle = new ActionBarDrawerToggle (this, drawerLayout, mToolbar, R.string.open, R.string.close);
// Setting the actionbarToggle to drawer layout
drawerLayout.setDrawerListener(mDrawerToggle);
// Calling sync state is necessary to show your hamburger icon...
// or so I hear. Doesn't hurt including it even if you find it works
// without it on your test device(s)
mDrawerToggle.syncState();
}
/**
* To be semantically or contextually correct, maybe change the name
* and signature of this function to something like:
*
* private void showBackButton(boolean show)
* Just a suggestion.
*/
private void enableViews(boolean enable) {
// To keep states of ActionBar and ActionBarDrawerToggle synchronized,
// when you enable on one, you disable on the other.
// And as you may notice, the order for this operation is disable first, then enable - VERY VERY IMPORTANT.
if(enable) {
//You may not want to open the drawer on swipe from the left in this case
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
// Remove hamburger
mDrawerToggle.setDrawerIndicatorEnabled(false);
// Show back button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// when DrawerToggle is disabled i.e. setDrawerIndicatorEnabled(false), navigation icon
// clicks are disabled i.e. the UP button will not work.
// We need to add a listener, as in below, so DrawerToggle will forward
// click events to this listener.
if(!mToolBarNavigationListenerIsRegistered) {
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Doesn't have to be onBackPressed
onBackPressed();
}
});
mToolBarNavigationListenerIsRegistered = true;
}
} else {
//You must regain the power of swipe for the drawer.
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
// Remove back button
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
// Show hamburger
mDrawerToggle.setDrawerIndicatorEnabled(true);
// Remove the/any drawer toggle listener
mDrawerToggle.setToolbarNavigationClickListener(null);
mToolBarNavigationListenerIsRegistered = false;
}
// So, one may think "Hmm why not simplify to:
// .....
// getSupportActionBar().setDisplayHomeAsUpEnabled(enable);
// mDrawer.setDrawerIndicatorEnabled(!enable);
// ......
// To re-iterate, the order in which you enable and disable views IS important #dontSimplify.
}
該解決方案使用 ActionBarDrawerToggle.setDrawerIndicatorEnabled 來切換漢堡包圖標(biāo)的可見性,使用 ActionBar.setDisplayHomeAsUpEnabled 來切換向上按鈕的可見性,本質(zhì)上是利用它們各自的可繪制資源。

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超3個(gè)贊
如果您認(rèn)為這是不好的解決方案,請(qǐng)告訴我。這就是我能夠使用非常簡單的解決方案(Kotlin)使其工作的方法:
將其放入您的活動(dòng) onCreate 中
supportFragmentManager.addOnBackStackChangedListener {
? ? ? ?actionBarDrawerToggle?.isDrawerIndicatorEnabled = supportFragmentManager.backStackEntryCount <= 0
}
并重寫 onSupportNavigateUp():
override fun onSupportNavigateUp(): Boolean {
? ? supportFragmentManager.popBackStack()
? ? return super.onSupportNavigateUp()
}

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超3個(gè)贊
您可以通過動(dòng)態(tài)地將工具欄添加到主活動(dòng)來完成此操作,而不是使用內(nèi)置的導(dǎo)航抽屜,從頭開始制作。添加片段時(shí)在片段上添加工具欄。工具欄 myChildToolbar = (工具欄) findViewById(R.id.my_child_toolbar); setSupportActionBar(myChildToolbar);
// Get a support ActionBar corresponding to this toolbar
ActionBar ab = getSupportActionBar();
// Enable the Up button
ab.setDisplayHomeAsUpEnabled(true);
然后,您將能夠重寫按鈕導(dǎo)航向上方法上的方法,以在工具欄上啟用您想要的后退按鈕。
添加回答
舉報(bào)