Android 删除 tabwidget 中选项卡之间的空格

发布于 2024-11-27 05:42:31 字数 4608 浏览 1 评论 0原文

我制作了一个应用程序,其中有像 HelloTabActivity 中那样的选项卡,这些选项卡之间也有空格,任何人都可以建议如何删除此空格,并且选项卡下方有一条灰线,如何删除?

在此处输入图像描述

ma​​in.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?

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

纵山崖 2024-12-04 05:42:31

要删除选项卡栏底部的灰线,您可以设置

tabHost.getTabWidget().setStripEnabled(false);

删除选项卡之间的间隙。最好的方法是使用您自己的可绘制对象,无需任何填充。您可以为此使用图像,也可以通过 xml 创建选项卡背景,例如在 根元素内:

<layer_list>
    <item android:top="0dp" android:left="0dp" android:right="0dp" android:bottom="0dp">
        <shape android:shape="rectangle">
        [..]
        </shape>
    </item>
    [..]
</layer_list>

并将此可绘制对象设置为 TabWidget< 的背景/代码>。

要了解如何自定义选项卡,网上有很多教程。例如,Josh 的这篇文章很短,并且有很好的解释。

更新

在这里,我分享了一个使用自定义选项卡(基于您的代码)的 tabwidget 的小示例,以实现以下输出:

custom tabs

您需要什么:

  1. 三个新的图层可绘制对象(用于
    选择、聚焦和未选择
    选项卡的状态)
  2. 两个状态可绘制对象(对于文本
    以及不同的背景
    状态)
  3. 选项卡的新布局
  4. 更新您的 main.xml
  5. 更新您的活动类
  6. 更新您的 androidManifest.xml
    (删除样式声明)

三层可绘制对象:tab_normal.xmltab_focused.xmltab_selected.xml
drawable/tab_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/default_back_color" />
        </shape>
    </item>
    <item android:top="2dp">
        <shape android:shape="rectangle">
            <gradient android:startColor="#AAAAAA" android:centerColor="#888888"
                android:endColor="#666666" android:angle="90" />
            <corners android:bottomRightRadius="0dp"
                android:bottomLeftRadius="0dp" android:topLeftRadius="10dp"
                android:topRightRadius="10dp" />
        </shape>
    </item>
    <item android:top="4dp" android:left="1dp" android:right="1dp"
        android:bottom="0dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/default_back_color" />
            <corners android:bottomRightRadius="0dp"
                android:bottomLeftRadius="0dp" android:topLeftRadius="8dp"
                android:topRightRadius="8dp" />
        </shape>
    </item>
</layer-list>

drawable/tab_focused.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/default_back_color" />
        </shape>
    </item>
    <item android:top="2dp">
        <shape android:shape="rectangle">
            <gradient android:startColor="#AAAAAA" android:centerColor="#888888"
                android:endColor="#666666" android:angle="90" />
            <corners android:bottomRightRadius="0dp"
                android:bottomLeftRadius="0dp" android:topLeftRadius="10dp"
                android:topRightRadius="10dp" />
        </shape>
    </item>
    <item android:top="4dp" android:left="1dp" android:right="1dp"
        android:bottom="0dp">
        <shape android:shape="rectangle">
            <gradient android:startColor="#8F8F8F" android:centerColor="#656565"
                android:endColor="#3F3F3F" android:angle="90" />
            <corners android:bottomRightRadius="0dp"
                android:bottomLeftRadius="0dp" android:topLeftRadius="8dp"
                android:topRightRadius="8dp" />
        </shape>
    </item>
</layer-list>

drawable/tab_selected.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/default_back_color" />
        </shape>
    </item>
    <item android:top="2dp">
        <shape android:shape="rectangle">
            <gradient android:startColor="#EEEEEE" android:centerColor="#CCCCCC"
                android:endColor="#AAAAAA" android:angle="-90" />
            <corners android:bottomRightRadius="0dp"
                android:bottomLeftRadius="0dp" android:topLeftRadius="10dp"
                android:topRightRadius="10dp" />
        </shape>
    </item>
    <item android:top="4dp" android:left="1dp" android:right="1dp"
        android:bottom="0dp">
        <shape android:shape="rectangle">
            <gradient android:startColor="#EAEAEA" android:centerColor="#9F9F9F"
                android:endColor="#696969" android:angle="90" />
            <corners android:bottomRightRadius="0dp"
                android:bottomLeftRadius="0dp" android:topLeftRadius="8dp"
                android:topRightRadius="8dp" />
        </shape>
    </item>
</layer-list>

两个状态drawable:tab_background_selector.xmltab_text_selector.xml
drawable/tab_background_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:state_focused="false"
        android:state_pressed="false" android:drawable="@drawable/tab_selected"/>
    <item android:state_selected="false" android:state_focused="false"
        android:state_pressed="false" android:drawable="@drawable/tab_normal" />
    <item android:state_pressed="true" android:drawable="@drawable/tab_focused"/>
    <item android:state_focused="true" android:state_selected="true"
        android:state_pressed="false" android:drawable="@drawable/tab_selected"/>
</selector>

drawable/tab_text_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="#1589FF" />
    <item android:state_focused="true" android:color="#1589FF" />
    <item android:state_pressed="true" android:color="@android:color/white" />
    <item android:color="#F0F0F0" />
</selector>

选项卡的新布局:tab.xml
layout/tab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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">
    <ImageView android:id="@+id/tab_icon" android:layout_width="30dp"
        android:layout_height="30dp" android:scaleType="fitCenter" />
    <TextView android:id="@+id/tab_text" android:layout_width="wrap_content"
        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" />
</LinearLayout>

ma​​in.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">
            <HorizontalScrollView android:scrollbars="none"
                android:layout_width="fill_parent" android:layout_height="wrap_content">
                <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>

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 文件中丢失角规范即可。
还可以尝试一下颜色、笔触等,以使结果符合您的喜好。

For removing the grey line at the bottom of your tabbar, you can set

tabHost.getTabWidget().setStripEnabled(false);

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:

<layer_list>
    <item android:top="0dp" android:left="0dp" android:right="0dp" android:bottom="0dp">
        <shape android:shape="rectangle">
        [..]
        </shape>
    </item>
    [..]
</layer_list>

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:

custom tabs

What you need:

  1. three new layer drawables (for
    selected, focused and unselected
    states of the tabs)
  2. two state drawables (for the text
    and background of the different
    states)
  3. a new layout for the tabs
  4. update your main.xml
  5. update your activity class
  6. update your androidManifest.xml
    (remove the style declarations)

The three layer drawables: tab_normal.xml, tab_focused.xml, tab_selected.xml
drawable/tab_normal.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/default_back_color" />
        </shape>
    </item>
    <item android:top="2dp">
        <shape android:shape="rectangle">
            <gradient android:startColor="#AAAAAA" android:centerColor="#888888"
                android:endColor="#666666" android:angle="90" />
            <corners android:bottomRightRadius="0dp"
                android:bottomLeftRadius="0dp" android:topLeftRadius="10dp"
                android:topRightRadius="10dp" />
        </shape>
    </item>
    <item android:top="4dp" android:left="1dp" android:right="1dp"
        android:bottom="0dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/default_back_color" />
            <corners android:bottomRightRadius="0dp"
                android:bottomLeftRadius="0dp" android:topLeftRadius="8dp"
                android:topRightRadius="8dp" />
        </shape>
    </item>
</layer-list>

drawable/tab_focused.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/default_back_color" />
        </shape>
    </item>
    <item android:top="2dp">
        <shape android:shape="rectangle">
            <gradient android:startColor="#AAAAAA" android:centerColor="#888888"
                android:endColor="#666666" android:angle="90" />
            <corners android:bottomRightRadius="0dp"
                android:bottomLeftRadius="0dp" android:topLeftRadius="10dp"
                android:topRightRadius="10dp" />
        </shape>
    </item>
    <item android:top="4dp" android:left="1dp" android:right="1dp"
        android:bottom="0dp">
        <shape android:shape="rectangle">
            <gradient android:startColor="#8F8F8F" android:centerColor="#656565"
                android:endColor="#3F3F3F" android:angle="90" />
            <corners android:bottomRightRadius="0dp"
                android:bottomLeftRadius="0dp" android:topLeftRadius="8dp"
                android:topRightRadius="8dp" />
        </shape>
    </item>
</layer-list>

drawable/tab_selected.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/default_back_color" />
        </shape>
    </item>
    <item android:top="2dp">
        <shape android:shape="rectangle">
            <gradient android:startColor="#EEEEEE" android:centerColor="#CCCCCC"
                android:endColor="#AAAAAA" android:angle="-90" />
            <corners android:bottomRightRadius="0dp"
                android:bottomLeftRadius="0dp" android:topLeftRadius="10dp"
                android:topRightRadius="10dp" />
        </shape>
    </item>
    <item android:top="4dp" android:left="1dp" android:right="1dp"
        android:bottom="0dp">
        <shape android:shape="rectangle">
            <gradient android:startColor="#EAEAEA" android:centerColor="#9F9F9F"
                android:endColor="#696969" android:angle="90" />
            <corners android:bottomRightRadius="0dp"
                android:bottomLeftRadius="0dp" android:topLeftRadius="8dp"
                android:topRightRadius="8dp" />
        </shape>
    </item>
</layer-list>

The two state drawables: tab_background_selector.xml, tab_text_selector.xml
drawable/tab_background_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:state_focused="false"
        android:state_pressed="false" android:drawable="@drawable/tab_selected"/>
    <item android:state_selected="false" android:state_focused="false"
        android:state_pressed="false" android:drawable="@drawable/tab_normal" />
    <item android:state_pressed="true" android:drawable="@drawable/tab_focused"/>
    <item android:state_focused="true" android:state_selected="true"
        android:state_pressed="false" android:drawable="@drawable/tab_selected"/>
</selector>

drawable/tab_text_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="#1589FF" />
    <item android:state_focused="true" android:color="#1589FF" />
    <item android:state_pressed="true" android:color="@android:color/white" />
    <item android:color="#F0F0F0" />
</selector>

The new layout for the tabs: tab.xml
layout/tab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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">
    <ImageView android:id="@+id/tab_icon" android:layout_width="30dp"
        android:layout_height="30dp" android:scaleType="fitCenter" />
    <TextView android:id="@+id/tab_text" android:layout_width="wrap_content"
        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" />
</LinearLayout>

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">
            <HorizontalScrollView android:scrollbars="none"
                android:layout_width="fill_parent" android:layout_height="wrap_content">
                <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>

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

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.

つ可否回来 2024-12-04 05:42:31

在 xml 布局中使用 android:showDividers="none" 。

<TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:showDividers="none"
        android:layout_height="wrap_content"/>

use android:showDividers="none" in xml layout.

<TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:showDividers="none"
        android:layout_height="wrap_content"/>
满栀 2024-12-04 05:42:31

如果您的构建目标是 Honeycomb 及以上版本,则可以使用以下代码。

if (Integer.parseInt(Build.VERSION.SDK) >= Build.VERSION_CODES.HONEYCOMB) {
    tabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
}

If your build target is Honeycomb onwards, you may use the following code.

if (Integer.parseInt(Build.VERSION.SDK) >= Build.VERSION_CODES.HONEYCOMB) {
    tabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
}
瞳孔里扚悲伤 2024-12-04 05:42:31

要消除选项卡之间的多余间隙,可以将 tabwidget 上的measureWithLargestChild 属性设置为 false

<TabWidget
   android:id="@android:id/tabs"                    
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:measureWithLargestChild="false">
 </TabWidget>

To remove the excess gap between the tabs one can set measureWithLargestChild attribute on tabwidget to false

<TabWidget
   android:id="@android:id/tabs"                    
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:measureWithLargestChild="false">
 </TabWidget>
红尘作伴 2024-12-04 05:42:31

一根魔线。

mTabHost.getTabWidget().setDividerDrawable(null);

完成

One magic line.

mTabHost.getTabWidget().setDividerDrawable(null);

Done

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文