2017-02-18 148 views
4

我听说我们无法在片段内运行Android活动。如何将选项卡式活动添加到导航抽屉活动中?

我开始了一个新项目Navigation Drawer Activity选择。然后,我需要的选项卡式活动为这样的默认视图:

enter image description here

如果我们不能在navigation drawer activity运行tabbed activity的片段再怎么加呢?

谢谢。

+0

制作一个片段,并在其中放置制表符布局,并在onCreate方法中打开它的活动方法 – AbhayBohra

+1

可能的重复方式.http://stackoverflow.com/questions/34917596/how-to-impliment-navigation-drawer-and- swipe-tab-in-same-activity-in-android-stu –

+1

这个答案已经在堆栈溢出中得到了解答.. http://stackoverflow.com/questions/34917596/how-to-impliment-navigation-drawer-and- swipe-tab-in-same-activity-in-android-stu –

回答

9

在导航抽屉activtiy改变你的布局是这样的:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
android:orientation="vertical" 
tools:context="in.net.spectrum.citytour.MainActivity"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay" /> 
    // 
    // 
    // Tab layout to show tabs 
    // 
    // 
    <android.support.design.widget.TabLayout 
     android:background="@color/colorPrimary" 
     app:tabSelectedTextColor="#ffffff" 
     app:tabIndicatorColor="#FFFFFF" 
     app:tabIndicatorHeight="3dp" 
     app:tabTextColor="#FFFFFF" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:tabMode="fixed" 
     app:tabGravity="fill" 
     android:id="@+id/tbl_pages"/> 

</android.support.design.widget.AppBarLayout> 
<!-- <include layout="@layout/content_main" />--> 
<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    // 
    // 
    //ViewPager to show tab's fragments 
    // 
    // 

    <android.support.v4.view.ViewPager 
     android:id="@+id/vp_pages" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    </android.support.v4.view.ViewPager> 


    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     app:srcCompat="@android:drawable/ic_dialog_email" /> 
</FrameLayout> 


</LinearLayout> 

现在在您的Activity中Java代码ins IDE的onCreate()添加这些行:

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
      . 
      . 
      . 
      . 

    ViewPager vp_pages= (ViewPager) findViewById(R.id.vp_pages); 
    PagerAdapter pagerAdapter=new FragmentAdapter(getSupportFragmentManager()); 
    vp_pages.setAdapter(pagerAdapter); 

    TabLayout tbl_pages= (TabLayout) findViewById(R.id.tbl_pages); 
    tbl_pages.setupWithViewPager(vp_pages); 

    } 

现在的onCreate外()方法创建片段适配器:

class FragmentAdapter extends FragmentPagerAdapter { 

    public FragmentAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     switch (position){ 
      case 0: 
       return new YourFragment1(); 
      case 1: 
       return new YourFragment2(); 
      case 2: 
       return new YourFragment3(); 
     } 
     return null; 
    } 

    @Override 
    public int getCount() { 
     return 3; 
    } 


    @Override 
    public CharSequence getPageTitle(int position) { 
     switch (position){ 
      // 
      //Your tab titles 
      // 
      case 0:return "Profile"; 
      case 1:return "Search"; 
      case 2: return "Contacts"; 
      default:return null; 
     } 
    } 
} 

输出: enter image description here enter image description here

干杯,编码愉快。

+0

哇!你给我完整的答案...让我试试... –

+0

你的XML文件在那个教程,是:content_main.xml或activity_main.xml? –

+0

app_bar_main.xml –

1

您可以通过以下步骤中添加选项卡活动:

  1. 在项目资源管理器右键单击软件包名称。
  2. 选择 “”,然后选择 “活动” 选项
  3. 然后选择 “选项卡式活动

Tabbed activity

相关问题