我用this教程创建了一些标签(第一个答案) 但是我不知道我应该把我的其他代码放在Tab1Fragment.java中! 我为Android编程这些是代码:
MainActivity.java
package com.example.applicationname;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
public class MainActivity extends FragmentActivity {
// Fragment TabHost as mTabHost
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab1"),
Tab1Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab2"),
Tab2Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab3"),
Tab3Fragment.class, null);
}
}
Tab1Fragment.java
package com.example.applicationname;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Tab1Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View V = inflater.inflate(R.layout.tab1_view, container, false);
return V;
}
}
答案 0 :(得分:0)
实施标签的最简单方法是通过 TabLayout 和 ViewPager 。
MainActivity
此代码将采用onStart / onCreate方法。
TabLayout tabLay = (TabLayout) findViewById(R.id.tabLayout);
tabLay.addTab(tabLay.newTab().setText("Collections"));
tabLay.addTab(tabLay.newTab().setText("Loyalty"));
tabLay.addTab(tabLay.newTab().setText("Orders"));
viewPager = (ViewPager) findViewById(R.id.userTabs);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLay.getTabCount());
viewPager.setAdapter(adapter);
tabLay.setupWithViewPager(viewPager);
tabLay.getTabAt(0).setText("Collections");
tabLay.getTabAt(0).setIcon(R.drawable.collage);
tabLay.getTabAt(1).setText("Loyalty");
tabLay.getTabAt(1).setIcon(R.drawable.heart_outline);
tabLay.getTabAt(2).setText("Orders");
tabLay.getTabAt(2).setIcon(R.drawable.cart_outline);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLay));
tabLay.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
寻呼机适配器
public class PagerAdapter extends FragmentStatePagerAdapter {
public int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = 3;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
CollectionsFragment tab1 = new CollectionsFragment();
return tab1;
case 1:
LoyaltyFragment tab2 = new LoyaltyFragment();
return tab2;
case 2:
OrdersFragment tab3 = new OrdersFragment();
return tab3;
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}}
片段
public class CollectionsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag_collections, container, false);
}
}
每个片段的实现都类似。
这应该这样做。