Android API级别< 4上缺少TabSpec.setIndicator(View view)的解决方法 [英] Workaround for the absence TabSpec.setIndicator(View view) on Android API level <4
本文介绍了Android API级别< 4上缺少TabSpec.setIndicator(View view)的解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将自定义视图用于TabHost的指示器.在Android API级别> = 4的情况下没有问题,但是在Android API级别< 4的情况下,不会实现此方法.有什么建议吗?
I need to use my custom view for the indicators of my TabHost. With Android API level >=4 no problem but in the Android API level <4 this method is not implemented. Any suggestion?
我当时正在考虑实现此方法,但是不幸的是TabHost类不允许更改,因为它具有所有属性均为私有属性且未受保护.
I was thinking to implement this method but unfortunately the TabHost class does not allow changes because has all attributes private and not protected.
谢谢.
推荐答案
我建议使用反射:
private void setIndecator(TabHost.TabSpec tabSpec, String label) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout tabView = (LinearLayout) vi.inflate(R.layout.tab_view, null);
((TextView)tabView.findViewById(R.id.tabCaption)).setText(label);
try {
Method m = tabSpec.getClass().getMethod("setIndicator", View.class);
m.invoke(tabSpec, tabView);
} catch (Exception e) {
//in case if platform 1.5 or via other problems indicator cannot be set as view
//we have to set as just simple label.
tabSpec.setIndicator(label, getResources().getDrawable(R.layout.tab_selector));
}
}
这篇关于Android API级别< 4上缺少TabSpec.setIndicator(View view)的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文