Android的未能使用tabhost [英] Android fail to use tabhost
问题描述
我写这篇code会对我的看法几个选项卡。现在的问题是:我只有一个标签,因为我一个异常,说:
I wrote this code to have several tabs on my view. The problem is : I've only one tab, because I've an exception which says :
DId you forget to call public void setup (LocalActivityManager aactivityGroup)
我的类:
MainActivity extends FragmentActivity (I don't use tabactivity because it's deprecated)
和我的code具有标签:
And my code to have tabs :
try{
Intent intent = new Intent(this, firstActivity.class);
tabHost = (TabHost) findViewById(R.id.tabhost);
tabHost.setup();
for (String tab_name : tabs) {
tabSpec = tabHost.newTabSpec(tab_name).
setIndicator(tab_name, getResources().getDrawable(R.drawable.test_logo))
.setContent(intent);
tabHost.addTab(tabSpec);
}
}
我看到了,我可以用tabHost.setup(LocalActivityManager的ActivityGroup),所以我创建一个对象localactivitymanager但它去precated,我又我只一个选项卡,出现异常:
I saw that I can use tabHost.setup(LocalActivityManager activityGroup), so I created an object localactivitymanager but it's deprecated , and i've again only ONE tab with exception :
Activityes can't be add until the containing group has been created.
所以,我真的不明白我需要做的....有人帮我PLZ?
So I really don't understand what I need to do.... someone to help me plz ?
推荐答案
这是viewpager。尝试这个..
指定您在getCount将功能多少标签需要
This is viewpager. Try this.. Specify how many tabs you need in the getcount function
public class ParentViewPagerFragment extends Fragment {
public static final String TAG = ParentViewPagerFragment.class.getName();
public static ParentViewPagerFragment newInstance() {
return new ParentViewPagerFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_parent_viewpager,
container, false);
ViewPager viewPager = (ViewPager) root.findViewById(R.id.viewPager);
/**
* Important: Must use the child FragmentManager or you will see side
* effects.
*/
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
return root;
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return 7;
}
@Override
public Fragment getItem(int position) {
Bundle args = new Bundle();
args.putInt(ChildFragment.POSITION_KEY, position);
return ChildFragment.newInstance(args);
}
@Override
public CharSequence getPageTitle(int position) {
position = position + 1;
return "Lesson " + position;
}
}
}
下面的标签改变位置你想要做什么......
Here on change position of tabs do what you want...
public class ChildFragment1 extends Fragment {
public static final String POSITION_KEY = "FragmentPositionKey";
private int position;
String htmlText,link;
WebView w;
public static ChildFragment1 newInstance(Bundle args) {
ChildFragment1 fragment = new ChildFragment1();
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
position = getArguments().getInt(POSITION_KEY);
View root = inflater.inflate(R.layout.fragment_child, container, false);
w = (WebView)root.findViewById(R.id.webView1);
if(position==0)
{
// your code here
}
if(position==1)
{
// your code here
}
if(position==2)
{
// your code here
}
if(position==3)
{
// your code here
}
return root;
}
}
这篇关于Android的未能使用tabhost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!