要删除标签栏底部的灰线,您可以设置
tabHost.getTabWidget().setStripEnabled(false);
消除标签之间的缝隙。最好的方法是使用自己的可绘制对象,不添加任何填充。您可以为此使用图像,也可以通过xml创建标签的背景,例如在根元素中:
[..]
[..]
并将此drawable设置为您的背景TabWidget。
要了解如何自定义标签,网络上有很多教程。例如,乔什(Josh)撰写的这篇文章简短且有很好的解释。
更新资料
在这里,我使用自定义标签(基于您的代码)分享了一个tabwidget的小样本,以实现以下输出:
自定义标签
您需要什么:
三个新的图层可绘制对象(用于选项卡的选定,集中和未选定状态)
两个状态可绘制对象(用于不同状态的文本和背景)
标签的新布局
更新你的 main.xml
更新您的活动课程
更新您的androidManifest.xml(删除样式声明)
这三个层可绘:tab_normal.xml,tab_focused.xml,tab_selected.xml
可拉伸/ tab_normal.xml:
android:endColor="#666666" android:angle="90" />
android:bottomLeftRadius="0dp" android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
android:bottom="0dp">
android:bottomLeftRadius="0dp" android:topLeftRadius="8dp"
android:topRightRadius="8dp" />
drawable / tab_focused.xml:
android:endColor="#666666" android:angle="90" />
android:bottomLeftRadius="0dp" android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
android:bottom="0dp">
android:endColor="#3F3F3F" android:angle="90" />
android:bottomLeftRadius="0dp" android:topLeftRadius="8dp"
android:topRightRadius="8dp" />
drawable / tab_selected.xml:
android:endColor="#AAAAAA" android:angle="-90" />
android:bottomLeftRadius="0dp" android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
android:bottom="0dp">
android:endColor="#696969" android:angle="90" />
android:bottomLeftRadius="0dp" android:topLeftRadius="8dp"
android:topRightRadius="8dp" />
两个状态可绘制对象:tab_background_selector.xml,tab_text_selector.xml
drawable / tab_background_selector.xml:
android:state_pressed="false" android:drawable="@drawable/tab_selected"/>
android:state_pressed="false" android:drawable="@drawable/tab_normal" />
android:state_pressed="false" android:drawable="@drawable/tab_selected"/>
drawable / tab_text_selector.xml:
选项卡的新布局:tab.xml
layout / tab.xml
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="@drawable/tab_background_selector" android:gravity="center"
android:orientation="vertical" android:padding="5dp">
android:layout_height="30dp" android:scaleType="fitCenter" />
android:layout_height="wrap_content" android:singleLine="true"
android:textStyle="bold" android:gravity="center_horizontal"
android:textSize="10sp" android:padding="3dip" android:ellipsize="marquee"
android:textColor="@drawable/tab_text_selector" />
main.xml:
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_height="fill_parent">
android:layout_width="fill_parent" android:layout_height="fill_parent">
android:layout_width="fill_parent" android:layout_height="wrap_content">
android:layout_width="fill_parent" android:layout_height="wrap_content" />
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="5dp" />
InfralineTabWidget.java:
public class InfralineTabWidget extends TabActivity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TabHost tabHost = (TabHost) getTabHost();
tabHost.addTab(createTab(TopNewsActivity.class,
"topNews", "Top News", R.drawable.tab_news));
tabHost.addTab(createTab(PowerActivity.class,
"power", "Power", R.drawable.tab_power));
tabHost.addTab(createTab(EnergyActivity.class,
"energy", "Renewable Energy", R.drawable.tab_energy));
tabHost.addTab(createTab(CoalActivity.class,
"coal", "Coal", R.drawable.tab_coal));
tabHost.addTab(createTab(OilnGasActivity.class,
"oilnGas", "Oil & Gas", R.drawable.tab_oilngas));
tabHost.setCurrentTab(0);
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;
}
private TabSpec createTab(final Class> intentClass, final String tag,
final String title, final int drawable)
{
final Intent intent = new Intent().setClass(this, intentClass);
final View tab = LayoutInflater.from(getTabHost().getContext()).
inflate(R.layout.tab, null);
((TextView)tab.findViewById(R.id.tab_text)).setText(title);
((ImageView)tab.findViewById(R.id.tab_icon)).setImageResource(drawable);
return getTabHost().newTabSpec(tag).setIndicator(tab).setContent(intent);
}
}
就是这样。
要创建直角选项卡,只需从图层可绘制的xml文件中丢失转角规格即可。
也可以在颜色,笔触等周围玩耍,以使结果适合您的喜好。