Android 删除 tabwidget 中选项卡之间的空格
我制作了一个应用程序,其中有像 HelloTabActivity 中那样的选项卡,这些选项卡之间也有空格,任何人都可以建议如何删除此空格,并且选项卡下方有一条灰线,如何删除?
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</HorizontalScrollView>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
</LinearLayout>
styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomTheme" parent="@android:style/Theme">
<item name="android:tabWidgetStyle">@style/CustomTabWidget</item>
</style>
<style name="CustomTabWidget" parent="@android:style/Widget.TabWidget">
<item name="android:textAppearance">@style/CustomTabWidgetText</item>
</style>
<style name="CustomTabWidgetText"
parent="@android:style/TextAppearance.Widget.TabWidget">
<item name="android:textSize">10sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#1589FF</item>
<item name="android:padding">3dip</item>
</style>
</resources>
活动
public class InfralineTabWidget extends TabActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = (TabHost)getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, TopNewsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("topNews").setIndicator("Top News", res.getDrawable(R.drawable.tab_news)).setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, PowerActivity.class);
spec = tabHost.newTabSpec("power").setIndicator("Power", res.getDrawable(R.drawable.tab_power)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, EnergyActivity.class);
spec = tabHost.newTabSpec("energy").setIndicator("Renewable Energy", res.getDrawable(R.drawable.tab_energy)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, CoalActivity.class);
spec = tabHost.newTabSpec("coal").setIndicator("Coal", res.getDrawable(R.drawable.tab_coal)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, OilnGasActivity.class);
spec = tabHost.newTabSpec("oilnGas").setIndicator("Oil & Gas", res.getDrawable(R.drawable.tab_oilngas)).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
tabHost.getTabWidget().getChildAt(0).setLayoutParams(new LinearLayout.LayoutParams(100,25));
tabHost.getTabWidget().getChildAt(1).setLayoutParams(new LinearLayout.LayoutParams(100,25));
tabHost.getTabWidget().getChildAt(2).setLayoutParams(new LinearLayout.LayoutParams(100,25));
tabHost.getTabWidget().getChildAt(3).setLayoutParams(new LinearLayout.LayoutParams(100,25));
tabHost.getTabWidget().getChildAt(4).setLayoutParams(new LinearLayout.LayoutParams(100,25));
}
}
现在我想删除选项卡之间的黑色空间,它们应该是连接的,而且我无法删除选项卡下方的灰线。
谢谢
I have made an application which has tabs like in HelloTabActivity, there is also space between these tabs, can anyone suggest how to remove this space and also there is a grey line beneath the tabs how can that be removed?
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</HorizontalScrollView>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
</LinearLayout>
styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomTheme" parent="@android:style/Theme">
<item name="android:tabWidgetStyle">@style/CustomTabWidget</item>
</style>
<style name="CustomTabWidget" parent="@android:style/Widget.TabWidget">
<item name="android:textAppearance">@style/CustomTabWidgetText</item>
</style>
<style name="CustomTabWidgetText"
parent="@android:style/TextAppearance.Widget.TabWidget">
<item name="android:textSize">10sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#1589FF</item>
<item name="android:padding">3dip</item>
</style>
</resources>
Activity
public class InfralineTabWidget extends TabActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = (TabHost)getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, TopNewsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("topNews").setIndicator("Top News", res.getDrawable(R.drawable.tab_news)).setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, PowerActivity.class);
spec = tabHost.newTabSpec("power").setIndicator("Power", res.getDrawable(R.drawable.tab_power)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, EnergyActivity.class);
spec = tabHost.newTabSpec("energy").setIndicator("Renewable Energy", res.getDrawable(R.drawable.tab_energy)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, CoalActivity.class);
spec = tabHost.newTabSpec("coal").setIndicator("Coal", res.getDrawable(R.drawable.tab_coal)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, OilnGasActivity.class);
spec = tabHost.newTabSpec("oilnGas").setIndicator("Oil & Gas", res.getDrawable(R.drawable.tab_oilngas)).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
tabHost.getTabWidget().getChildAt(0).setLayoutParams(new LinearLayout.LayoutParams(100,25));
tabHost.getTabWidget().getChildAt(1).setLayoutParams(new LinearLayout.LayoutParams(100,25));
tabHost.getTabWidget().getChildAt(2).setLayoutParams(new LinearLayout.LayoutParams(100,25));
tabHost.getTabWidget().getChildAt(3).setLayoutParams(new LinearLayout.LayoutParams(100,25));
tabHost.getTabWidget().getChildAt(4).setLayoutParams(new LinearLayout.LayoutParams(100,25));
}
}
Now i want to remove the black spaces between the tabs and it should be like they are connected and also i'm not able to remove the grey line below the tabs.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
要删除选项卡栏底部的灰线,您可以设置
删除选项卡之间的间隙。最好的方法是使用您自己的可绘制对象,无需任何填充。您可以为此使用图像,也可以通过 xml 创建选项卡背景,例如在
根元素内:并将此可绘制对象设置为
TabWidget< 的背景/代码>。
要了解如何自定义选项卡,网上有很多教程。例如,Josh 的这篇文章很短,并且有很好的解释。
更新
在这里,我分享了一个使用自定义选项卡(基于您的代码)的 tabwidget 的小示例,以实现以下输出:
您需要什么:
选择、聚焦和未选择
选项卡的状态)
以及不同的背景
状态)
main.xml
(删除样式声明)
三层可绘制对象:
tab_normal.xml
、tab_focused.xml
、tab_selected.xml
drawable/tab_normal.xml:
drawable/tab_focused.xml:
drawable/tab_selected.xml:
两个状态drawable:
tab_background_selector.xml
,tab_text_selector.xml
drawable/tab_background_selector.xml:
drawable/tab_text_selector.xml:
选项卡的新布局:
tab.xml
layout/tab.xml
main.xml:
InfralineTabWidget.java:
就是这样。
要创建直角选项卡,只需从图层可绘制 xml 文件中丢失角规范即可。
还可以尝试一下颜色、笔触等,以使结果符合您的喜好。
For removing the grey line at the bottom of your tabbar, you can set
As of removing the gap between the tabs.. The best way would be to use your own drawable without any paddings. You can use images for this, or you can create your tabs' backgrounds via xml's, say inside a
<layer_list>
root element:and set this drawable to be the background of your
TabWidget
.To see how to customize your tabs, there are a lot of tutorials on the web. For example this one by Josh is short and has a nice explanation.
Update
Here I share a small sample of tabwidget using custom tabs (based on your code) to achieve the following output:
What you need:
selected, focused and unselected
states of the tabs)
and background of the different
states)
main.xml
(remove the style declarations)
The three layer drawables:
tab_normal.xml
,tab_focused.xml
,tab_selected.xml
drawable/tab_normal.xml:
drawable/tab_focused.xml:
drawable/tab_selected.xml:
The two state drawables:
tab_background_selector.xml
,tab_text_selector.xml
drawable/tab_background_selector.xml:
drawable/tab_text_selector.xml:
The new layout for the tabs:
tab.xml
layout/tab.xml
main.xml:
InfralineTabWidget.java:
And this is it.
To create straight cornered tabs, just lose the corner specifications from the layer drawable xml files.
Also play around the colors, strokes, etc., to make the outcome fit your preferences.
在 xml 布局中使用 android:showDividers="none" 。
use android:showDividers="none" in xml layout.
如果您的构建目标是 Honeycomb 及以上版本,则可以使用以下代码。
If your build target is Honeycomb onwards, you may use the following code.
要消除选项卡之间的多余间隙,可以将 tabwidget 上的measureWithLargestChild 属性设置为 false
To remove the excess gap between the tabs one can set measureWithLargestChild attribute on tabwidget to false
一根魔线。
完成
One magic line.
Done