1

如何在选项卡小部件中设置自定义或默认选项卡分隔符。我已经尝试下面的代码来创建自定义选项卡小部件。

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

请帮助我在选项卡小部件中设置自定义分隔线..

提前致谢。

4

2 回答 2

4

您要在哪个版本上运行?

setDividerDrawable() 获取要用于分隔线的可绘制对象的 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
于 2013-07-25T18:59:11.883 回答
1

试试这个。

    mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
于 2013-11-20T05:43:27.200 回答