【问题标题】:android - How to add a NavigationDrawer within an activity that has a BottomNavigationBar with just one Toolbar?android - 如何在只有一个工具栏的具有底部导航栏的活动中添加 NavigationDrawer?
【发布时间】:2018-01-07 09:29:08
【问题描述】:

我有一个 MainActivity 包含:

1- BottomNavigationBar 和 5 个标签,包含 5 个片段。

2- 主旋律NoActionBar

3- 每个片段都有自己的Toolbar 和不同的title 的布局。

现在我想在 MainActivity 中添加一个NavigationDrawer,并且只有一个包含(BottomNavigationBar 选项卡的片段的标题和 NavDrawer 图标按钮)的 ActionBar,这一切都在 one ActionBar 中。 这张图片定义了我遇到的问题:

【问题讨论】:

    标签: android android-fragments navigation-drawer android-fragmentactivity bottomnavigationview


    【解决方案1】:

    好吧,如果您只想在片段工具栏上显示标题和图标,则不需要为每个片段设置工具栏。只需在主活动中使用一个,并在选项卡更改时更改标题和图标。

    使用这个:

    public class MainActivity extends AppCompatActivity {
    
     public BottomBar bottomBar;
     private Toolbar toolbar;
     private ImageView toolbarIcon;
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setTitle(" "); 
    
        bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
              @Override
              public void onTabSelected(@IdRes int tabId) {
    
                switch (tabId) {
                  case R.id.tab1: 
                    TabOne tabOne= new TabOne();
                    commitFragment(tabOne);
                    toolbar.setTitle("Tab One");
                    toolbarIcon.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.ic_tab_1));
                    break;
    
                  case R.id.tab1: 
                    TabTwo tabTwo= new TabTwo ();
                    commitFragment(tabTwo);
                    toolbar.setTitle("Tab Two");
                    toolbarIcon.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.ic_tab_2));
                    break;
    
                   // and so on....
                }
              }
            });
         }
       }
    

    并在工具栏中添加ImageView 以更好地控制更改。

    <android.support.v7.widget.Toolbar
      android:id="@+id/toolbar"
      android:layout_width="match_parent"
      android:layout_height="?attr/actionBarSize"
      android:background="@color/colorPrimary">
    
      <ImageView
        android:id="@+id/back_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="start" 
        android:scaleType="fitXY"/>
    
    </android.support.v7.widget.Toolbar>
    

    【讨论】:

      猜你喜欢
      • 2018-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2012-03-26
      相关资源
      最近更新 更多