使用navigationDrawerActivity向应用添加标签页

时间:2015-12-24 13:37:47

标签: java android tabs android-navigation-drawer

我是Android的新手,我正在尝试通过在android studio中制作一个简单的聊天应用来学习。

我开始创建一个导航抽屉。现在我想在我的应用程序中添加标签,例如this - 在导航抽屉下面的聊天/联系/调用,但是我找不到一个简单的分步教程来执行此操作。

在我开始使用此聊天应用之前,我使用了一个更简单的应用,并为这个旧应用添加了一些标签,如下所示:

    public class MainActivity extends TabActivity {

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Resources resources = getResources();
    TabHost tabHost = getTabHost();

    // First tab
    Intent intentTabOne = new Intent().setClass(this, TabOneActivity.class);
    TabSpec tabSpecTabOne = tabHost
            .newTabSpec("Tab One")
            .setIndicator("", resources.getDrawable(R.drawable.icon_one_config))
            .setContent(intentTabOne);

    // Second tab
    Intent intentTabTwo = new Intent().setClass(this, TabTwoActivity.class);
    TabSpec tabSpecSecondTab = tabHost
            .newTabSpec("Tab Two")
            .setIndicator("", resources.getDrawable(R.drawable.icon_two_config))
            .setContent(intentTabTwo);

    // Third tab
    Intent intentTabThree = new Intent().setClass(this, TabThree.class);
    TabSpec tabSpecSent = tabHost
            .newTabSpec("Tab Three")
            .setIndicator("", resources.getDrawable(R.drawable.icon_three_invitations_config))
            .setContent(intentTabThree);

    // add all tabs
    tabHost.addTab(tabSpecTabOne);
    tabHost.addTab(tabSpecTabTwo);
    tabHost.addTab(tabSpecTabThree);

    //set Windows tab as default 
    tabHost.setCurrentTab(0);
    }

但现在我的主要活动如下:

    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);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    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.setDrawerListener(toggle);
    toggle.syncState();

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

    @Override
    public void onBackPressed()...

此外,由于TabActivity现已弃用,我认为我应该使用片段,我有点迷失。如果有人能指出我正确的方向,我真的很感激。提前谢谢。

1 个答案:

答案 0 :(得分:2)

我建议您使用可以放在工具栏/操作栏下方的TabLayout,这样就可以与导航栏并排使用。

This answer显示如何使用ViewPager设置Tabbar。简而言之:您需要包含设计支持库才能使用TabLayout。在XML布局中,将TabLayout和ViewPager放在工具栏下方的某个位置,以使其符合您的意图。然后,您可以在活动中使用自己的片段设置不同的标签。