2016-01-24 53 views
0

我正在使用設計支持庫來創建選項卡。我添加了工具欄和標籤視圖佈局。使用設計支持庫將微調項添加爲Tab項目

我想添加微調控件作爲TAB與設計支持庫之一。

是否可以添加微調作爲TAB項目之一?你能舉個例子嗎?

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
 
    android:id="@+id/drawer_layout" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    android:fitsSystemWindows="true"> 
 

 
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
 
     android:id="@+id/rootLayout" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent"> 
 

 
     <android.support.design.widget.AppBarLayout 
 
      android:layout_width="match_parent" 
 
      android:layout_height="wrap_content" 
 
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 
 

 
      <android.support.v7.widget.Toolbar 
 
       android:id="@+id/toolbar" 
 
       android:layout_width="match_parent" 
 
       android:layout_height="?attr/actionBarSize" 
 
       android:background="?attr/colorPrimary" 
 
       app:layout_scrollFlags="scroll|enterAlways" 
 
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 
 

 
      <FrameLayout 
 
       android:id="@+id/flContent" 
 
       android:layout_width="match_parent" 
 
       android:layout_height="match_parent" /> 
 

 
      <android.support.design.widget.TabLayout 
 
       android:id="@+id/tab_layout" 
 
       android:layout_width="match_parent" 
 
       android:layout_height="wrap_content" /> 
 
     </android.support.design.widget.AppBarLayout> 
 

 
     <android.support.v4.view.ViewPager 
 
      android:id="@+id/view_pager" 
 
      android:layout_width="match_parent" 
 
      android:layout_height="match_parent" 
 
      app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 
 
    </android.support.design.widget.CoordinatorLayout> 
 

 
    <android.support.design.widget.NavigationView 
 
     android:id="@+id/navigation_view" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="match_parent" 
 
     android:layout_gravity="start" 
 
     android:background="@android:color/white" 
 
     app:headerLayout="@layout/layout_drawer_header" 
 
     app:itemTextColor="#000000" 
 
     app:menu="@menu/global"></android.support.design.widget.NavigationView> 
 
</android.support.v4.widget.DrawerLayout>

public class MainActivity extends AppCompatActivity { 
 

 
    private ActionBarDrawerToggle drawerToggle; 
 
    private DrawerLayout drawerLayout; 
 
    CoordinatorLayout rootLayout; 
 

 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.fragment_main); 
 
     initToolbar(); 
 
     //getSupportFragmentManager().beginTransaction().replace(R.id.flContent, new LettersFragment()).commit(); 
 
     rootLayout = (CoordinatorLayout) findViewById(R.id.rootLayout); 
 
     drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
 
     drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.opendrawer, 
 
       R.string.closedrawer); 
 
     drawerLayout.setDrawerListener(drawerToggle); 
 

 
     NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view); 
 
     if (navigationView != null) { 
 
      setupDrawerContent(navigationView); 
 
     } 
 

 
     final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager); 
 
     setupViewPager(viewPager); 
 

 
     TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); 
 
     tabLayout.setupWithViewPager(viewPager); 
 
     tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
 
      @Override 
 
      public void onTabSelected(TabLayout.Tab tab) { 
 

 
       viewPager.setCurrentItem(tab.getPosition()); 
 

 
       switch (tab.getPosition()) { 
 
        case 0: 
 
         showToast("One"); 
 
         break; 
 
        case 1: 
 
         break; 
 
        case 2: 
 
         showToast("Three"); 
 

 
         break; 
 
       } 
 
      } 
 

 
      @Override 
 
      public void onTabUnselected(TabLayout.Tab tab) { 
 

 
      } 
 

 
      @Override 
 
      public void onTabReselected(TabLayout.Tab tab) { 
 

 
      } 
 
     }); 
 
    } 
 

 
    void showToast(String msg) { 
 
     Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); 
 
    } 
 

 
    private void setupViewPager(ViewPager viewPager) { 
 
     ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); 
 
     adapter.addFrag(new DummyFragment(getResources().getColor(R.color.accent_material_light)), "ENGLISH"); 
 
     adapter.addFrag(new DummyFragment(getResources().getColor(R.color.ripple_material_light)), "TELUGU"); 
 
     viewPager.setAdapter(adapter); 
 
    } 
 
}

回答

0
 TabLayout tabLayout = findViewById(R.id.tab_layout); 
     TabLayout.Tab tab = tabLayout.newTab(); 
     Spinner spinner = new Spinner(MainActivity.this); 
//  spinner.setAdapter(new SimpleAdapter()); 
     tab.setCustomView(spinner); 
     tabLayout.addTab(tab); 
+0

我更新我的職務。我無法設置「setCustomView」。請參閱上面的代碼並幫助我。 –

+0

更改tabLayout.setupWithViewPager(viewPager);逐個添加Tab。 –