您的MainActivity擴展到AppCompatActivity
,它會去,直到Android版本7, http://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html
使用tabLayout和和查看傳呼機,要獲得輕掃對你的標籤
activity_main.xml
將以下內容添加到MainActivity佈局文件中。
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:visibility="visible">
</android.support.v4.view.ViewPager>
MainActivity.java
public class MainActivity extends AppCompatActivity{
ViewPager viewPager;
FragmentPagerAdapter ft;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewpaggermain);
viewPager = (ViewPager) findViewById(R.id.vp_main);
ft = new FragmentPagerAdapter(getSupportFragmentManager()) {
String [] titles = {"Subscriber","Merchant"};
final int VIEW_COUNT = 2;
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new YourFirstFragment();
case 1:
return new YourSecondFragment();
}
return null;
}
@Override
public int getCount() {
return VIEW_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
};
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
viewPager.setAdapter(ft);
tabLayout.setupWithViewPager(viewPager);
}
}
感謝asadnwfp,但我不知道這個回答我的問題哪一個是當前的首選方法和其API。這是API 23的現行標準嗎? – Jazzmine
@Jazzmine AppCompat用於向後兼容,使用取決於您的應用程序的用戶,可能,..它不是一個標準,但。 – asadnwfp