gpt4 book ai didi

android - Android API 级别 <4 上缺少 TabSpec.setIndicator(View view) 的解决方法

转载 作者:行者123 更新时间:2023-11-29 22:32:10 25 4
gpt4 key购买 nike

我需要为 TabHost 的指标使用我的自定义 View 。使用 Android API 级别 >=4 没问题,但在 Android API 级别 <4 中,此方法未实现。有什么建议吗?

我正在考虑实现此方法,但不幸的是 TabHost 类不允许更改,因为所有属性都是私有(private)的且不 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 - Android API 级别 <4 上缺少 TabSpec.setIndicator(View view) 的解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3618588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com