【问题标题】:Workaround for the absence TabSpec.setIndicator(View view) on Android API level <4Android API 级别 <4 上缺少 TabSpec.setIndicator(View view) 的解决方法
【发布时间】:2010-09-01 13:32:54
【问题描述】:

我需要为我的 TabHost 的指标使用我的自定义视图。 Android API 级别 >=4 没有问题,但在 Android API 级别

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

谢谢。

【问题讨论】:

    标签: android android-tabhost


    【解决方案1】:

    我建议使用反射:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多