如何在android中的自定义Tab Widget中设置Tab分隔符

时间:2013-06-10 10:32:25

标签: android tabwidget

如何在标签窗口小部件中设置自定义或默认标签分隔线。我尝试过以下代码来创建自定义选项卡小部件。

private void setTabs()
{
    TabHost tabhost=getTabHost();

    tabhost.getTabWidget().setDividerDrawable(TabWidget.SHOW_DIVIDER_MIDDLE);//here Application crashes

    tabhost.addTab(createTab(FoodTabGroup.class, "foods", "Foods", R.drawable.tab_foods));
    tabhost.addTab(createTab(BeveragesTabGroup.class, "beverages", "Beverages", R.drawable.tab_beverages));
    tabhost.addTab(createTab(DessertsTabGroup.class, "desserts", "Desserts", R.drawable.tab_desserts));
    tabhost.addTab(createTab(WinesTabGroup.class, "wines", "Wines", R.drawable.tab_wines));
    tabhost.addTab(createTab(OrderTabGroup.class, "order", "Order", R.drawable.tab_order));

    tabhost.getTabWidget().getChildAt(0).getLayoutParams().width = 140;
    tabhost.getTabWidget().getChildAt(1).getLayoutParams().width = 140;
    tabhost.getTabWidget().getChildAt(2).getLayoutParams().width = 140;
    tabhost.getTabWidget().getChildAt(3).getLayoutParams().width = 140;
    tabhost.getTabWidget().getChildAt(4).getLayoutParams().width = 140;

    tabhost.getTabWidget().getChildAt(0).getLayoutParams().height= 150;
    tabhost.getTabWidget().getChildAt(1).getLayoutParams().height= 150;
    tabhost.getTabWidget().getChildAt(2).getLayoutParams().height= 150;
    tabhost.getTabWidget().getChildAt(3).getLayoutParams().height= 150;
    tabhost.getTabWidget().getChildAt(4).getLayoutParams().height= 150;

    tabhost.setCurrentTab(0);
}

private TabSpec createTab(final Class<?> intentClass, final String tag, 
        final String title, final int drawable)
{
    final Intent intent = new Intent();
    intent.setClass(this, intentClass);

    final View tab = LayoutInflater.from(getTabHost().getContext()).
        inflate(R.layout.tab, null);
    TextView txtTab=(TextView)tab.findViewById(R.id.tab_text);
    txtTab.setText(title);
    txtTab.setPadding(8, 9, 8, 9);
    txtTab.setTextColor(Color.WHITE);
    txtTab.setTextSize(18);

    ImageView imgTab=(ImageView)tab.findViewById(R.id.tab_icon);
    imgTab.getLayoutParams().height=80;
    imgTab.getLayoutParams().width=80;
    imgTab.setImageResource(drawable);

    return getTabHost().newTabSpec(tag).setIndicator(tab).setContent(intent);
}

请帮我在标签小部件中设置自定义分隔符。

提前致谢。

2 个答案:

答案 0 :(得分:4)

您试图在哪个版本上运行?

setDividerDrawable()获取您想要用于分隔符的drawable的id。您传递的参数实际上与setShowDividers()一起使用,它告诉它在哪里绘制分隔符。

替换...

tabhost.getTabWidget().setDividerDrawable(TabWidget.SHOW_DIVIDER_MIDDLE);

...与

tabhost.getTabWidget().setShowDividers(TabWidget.SHOW_DIVIDER_MIDDLE);
tabhost.getTabWidget().setDividerDrawable( ... ); //id of your drawble resource here

答案 1 :(得分:1)

试试这个。

    mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);