我想创建一个自定义tabhost,它也包含一个图标。为此我需要使用一种方法。此方法工作正常,但选项卡指示符从选项卡中消失。
标签指示符的代码是:
private View getTabIndicator(Context context, String title, int icon) {
View view = LayoutInflater.from(context).inflate(R.layout.tab_layout, null);
ImageView iv = (ImageView) view.findViewById(R.id.imageView);
iv.setImageResource(icon);
TextView tv = (TextView) view.findViewById(R.id.textView);
tv.setText(title);
return view;
}
主要活动是:
public class MainActivity extends AppCompatActivity {
private FragmentTabHost mTabHost;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Setting up a toolbar for the navigation purpose.
toolbar = (Toolbar)findViewById(R.id.app_bar);
toolbar.setTitle(" Call History Control");
toolbar.setLogo(R.mipmap.ic_launcher);
setSupportActionBar(toolbar);
// The fragments management is done here
// mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
// mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
//
// mTabHost.addTab(
// mTabHost.newTabSpec("home").setIndicator(" HOME"),
// Home.class, null);
// mTabHost.addTab(mTabHost.newTabSpec("settings").setIndicator(" SETTINGS"),
// Settings.class, null);
//
// mTabHost.addTab(mTabHost.newTabSpec("about").setIndicator(" ABOUT"),
// About.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("home").setIndicator(getTabIndicator(getApplicationContext(),"HOME",R.drawable.ic_home)),
Home.class, null);
mTabHost.addTab(mTabHost.newTabSpec("settings").setIndicator(getTabIndicator(getApplicationContext(),"SETTINGS",R.drawable.ic_settings)),
Settings.class, null);
mTabHost.addTab(mTabHost.newTabSpec("about").setIndicator(getTabIndicator(getApplicationContext(),"ABOUT",R.drawable.ic_account)),
About.class, null);
}