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

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

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

提前致谢。

最佳答案

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

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

关于android - 如何在android中的自定义Tab Widget中设置Tab分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17021825/

相关文章:

java - Android中的时间粒度是多少?

python - 在 Pyqt 中选择选项卡时未发出信号

android - 如何去掉 2.1 中 Tab 下的线?

android - 为什么键盘出现在 TabBar 之后?

Android TabWidget 删除底部的行

android - 如何创建中心凸起的标签栏?

java - 即使 listview 获取了一个项目,Textview 仍然可见

android - 什么进入源代码管理?

java - 如何在 libgdx 中为从 1 个角到触摸点的 Sprite 表/纹理区域设置动画?

java - 如何在android中以字符串和日期格式获取明天日期以便在sqlite查询中使用?