2017-08-06 148 views
0

您好我製作刷卡選項卡應用程序,它適用於我滑動屏幕時,但我沒有看到選項卡部分。我該如何解決它? 你能幫助我嗎? 感謝看不到刷卡選項卡標籤

這裏是我的XML佈局

<RelativeLayout 
    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" 
    tools:context="com.example.hakobm.tabdemo.MainActivity"> 

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

     <include 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      layout="@layout/toolbar_layout"/> 

    </android.support.design.widget.AppBarLayout> 

    <android.support.design.widget.TabLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/tablayout" 
     app:tabMode="fixed" 
     app:tabGravity="fill" 
     /> 


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

    </android.support.v4.view.ViewPager> 

</RelativeLayout> 

這裏是我的MainActivity文件

public class MainActivity extends AppCompatActivity { 

    Toolbar toolbar; 
    TabLayout tabLayout; 
    ViewPager viewPager; 
    ViewPagerAdapter viewPagerAdapter; 

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


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

     tabLayout= (TabLayout) findViewById(R.id.tablayout); 
     viewPager= (ViewPager) findViewById(R.id.viewpager); 


     viewPagerAdapter=new ViewPagerAdapter(getSupportFragmentManager()); 
     viewPagerAdapter.addFragments(new Home(),"Home"); 
     viewPagerAdapter.addFragments(new Top(),"Top"); 
     viewPagerAdapter.addFragments(new Buttom(),"Buttom"); 
     viewPager.setAdapter(viewPagerAdapter); 
     tabLayout.setupWithViewPager(viewPager); 
     tabLayout.setTabsFromPagerAdapter(viewPagerAdapter); 

     viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 

    } 
} 

這是我的適配器部分:

public class ViewPagerAdapter extends FragmentStatePagerAdapter { 

    private ArrayList<Fragment> fragments= new ArrayList<>(); 
    private ArrayList<String> tabTitles = new ArrayList<>(); 

    public void addFragments(Fragment fragments, String titles){ 

     this.fragments.add(fragments); 
     this.tabTitles.add(titles); 

    } 

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

    @Override 
    public Fragment getItem(int position) { 
     return fragments.get(position); 
    } 

    @Override 
    public int getCount() { 
     return fragments.size(); 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return tabTitles.get(position); 
     // return "Tab" + position; 
    } 
} 

我不能看我的標籤無法使用的原因。 我加編譯 'com.android.support:design:25.3.1' 到我grandle.app文件

回答

1

試試這個:

添加一個ID,appbarlayout這樣的:

android:id="@+id/appbar" 

這增加了標籤

android:layout_below="@+id/appbar" 
+0

感謝,它幫助 –

相關問題