2015-03-02 90 views
3

我使用工具欄小部件作爲Cris Barnes指南後面的ActionBar。 在我的用例中,我需要隱藏活動中的導航抽屜,同時滑動到ViewPager中包含的另一個片段。 之前,我在使用ActionBar Widget來隱藏導航Drawer時使用了以下屬性。這似乎工作正常。 getActionBar().setDisplayHomeAsUpEnabled(false); getActionBar().setHomeButtonEnabled(false);如何在使用appcompat21時隱藏導航抽屜指示器

雖然現在使用

getSupportActionBar().setDisplayHomeAsUpEnabled(false); 
getSupportActionBar().setHomeButtonEnabled(false); 

時,這似乎並沒有隱藏ActionBar中的抽屜式導航欄更改爲AppCompat21。 我在這方面缺少的東西,任何幫助表示讚賞。

回答

10
toolbar.setNavigationIcon(null); 

它會隱藏導航圖標,以供參考您可以檢查此answer

+0

謝謝,這似乎隱藏導航圖標,但我沒有使用setIcon的自定義圖標。那麼,我將如何獲取導航抽屜指標。 – harshitpthk 2015-03-02 07:06:18

+0

感謝它使用toolbar.setNavigationIcon(getResources()。getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha)); – harshitpthk 2015-03-02 07:15:31

1

當你用你的代碼應該只工作:

getSupportActionBar().setDisplayHomeAsUpEnabled(false); 

getSupportActionBar().setHomeButtonEnabled(false); 

你也可以試試:

toolbar.setNavigationIcon(null); 
+1

謝謝toolbar.setNavi ..似乎隱藏它,但爲了顯示它回來ihad使用。 toolbar.setNavigationIcon(getResources()getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha)); – harshitpthk 2015-03-02 07:16:30

3

如果您正在使用ToolbarDrawerLayout內 - >AppBarLayout

那麼類

ActionBarDrawerToggle-->setDrawerIndicatorEnabled(false) 

功能將使導航抽屜圖標invisible.like

public class MainActivity extends AppCompatActivity 
       implements NavigationView.OnNavigationItemSelectedListener 
{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.addDrawerListener(toggle); 
//the below line of code will allow you to hide the nav drawer icon 
     toggle.setDrawerIndicatorEnabled(false);  
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
    } 
+0

當我嘗試這個時,我得到一個後退箭頭圖標而不是漢堡(3行)圖標。 :-( – 2017-09-23 00:11:03

+0

... oops。發生這種情況是因爲我還有'actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setHomeButtonEnabled(true);'。刪除它們,現在它工作得很好,10分!:-) – 2017-09-23 00:14:53