Workaround for the absence TabSpec.setIndicator(View view) on Android API level <4

ぃ、小莉子 提交于 2019-12-11 10:06:17

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/3618588/workaround-for-the-absence-tabspec-setindicatorview-view-on-android-api-level

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!