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

发布于 2024-09-17 17:56:43 字数 170 浏览 4 评论 0 原文

我需要将自定义视图用于 TabHost 的指示器。对于 Android API 级别 >=4 没有问题,但在 Android API 级别 <4 中此方法未实现。有什么建议吗?

我正在考虑实现此方法,但不幸的是 TabHost 类不允许更改,因为所有属性均为私有且不受保护。

谢谢。

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?

I was thinking to implement this method but unfortunately the TabHost class does not allow changes because has all attributes private and not protected.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺深海 2024-09-24 17:56:43

我建议使用反射:

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));
    }
}

I suggest use reflection:

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));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文