FragmentTabHost和自定义选项卡

时间:2014-01-03 16:30:29

标签: android tabs android-support-library

我真的需要在运行时访问选项卡的图像以进行缩放。 因此,我使用imageview和textview制作了自定义标签布局。

但是如果我想将自定义标签添加到我的tabhost中,我会“忘记调用'public void setup(localactivitymanager activitygroup)'”例外。

提前解决任何问题; D

聚苯乙烯。我不能用

spec = tabHost.newTabSpec("2").setIndicator(res.getString(R.string.tabname2), 
                                        res.getDrawable(R.drawable.tabimage2));

tabHost.addTab(spec,FragmentTwo.class,null);

因为我需要在添加之前缩放图像。 这是我的班级:

public class MyTabActivity extends FragmentActivity  {

private FragmentTabHost mTabHost;


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


     mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);

     mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

     addCustomTab(getApplicationContext(),"Meldungen", getResources().getDrawable(R.drawable.messagewhite), NewsFragment.class, mTabHost);
     addCustomTab(getApplicationContext(),"Meldungen", getResources().getDrawable(R.drawable.messagewhite), NewsFragment.class, mTabHost);
     addCustomTab(getApplicationContext(),"Meldungen", getResources().getDrawable(R.drawable.messagewhite), NewsFragment.class, mTabHost);
     addCustomTab(getApplicationContext(),"Meldungen", getResources().getDrawable(R.drawable.messagewhite), NewsFragment.class, mTabHost);

     for(int i=0;i<mTabHost.getTabWidget().getChildCount();i++)
     {
         mTabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#3b6c8d")); //unselected

     }

}

   private void addCustomTab(Context context,String labelId, Drawable drawable, Class<?> c, FragmentTabHost fth ) {
       View view = LayoutInflater.from(context).inflate(R.layout.tab_customtab, null);
       ImageView image = (ImageView) view.findViewById(R.id.icon);
       TextView text = (TextView) view.findViewById(R.id.tabtitle);

       image.setImageDrawable(drawable);
       text.setText(labelId);

       TabHost.TabSpec spec = fth.newTabSpec(labelId);
       Intent i = new Intent(this,c);
       spec.setContent(i);
       spec.setIndicator(view);
       fth.addTab(spec);


    }

1 个答案:

答案 0 :(得分:1)

您需要使用FragmentTabHost的自定义addTab(),这也会将您的Fragment类作为参数。

替换

TabHost.TabSpec spec = fth.newTabSpec(labelId);
Intent i = new Intent(this,c);
spec.setContent(i);
spec.setIndicator(view);

TabHost.TabSpec spec = fth.newTabSpec(labelId);
spec.setIndicator(view);
fth.addTab(spec, c, null);