无图标的Android选项卡

7

我已经按照以下方式设置了我的选项卡:

 spec = tabHost.newTabSpec("tab1").setIndicator("Tab 1").setContent(intent);
    tabHost.addTab(spec);

现在我有一个没有图标的选项卡,只有一个标题,但它会在底部留下一个图标大小的空白空间 - 我尝试调整xml中的layout_height,但那么文字就消失了,因为它被渲染在截断点之下。

我如何改变选项卡的大小,并显示没有图标的标题?


可能是没有图标的选项卡的重复问题。 - Kristopher Johnson
4个回答

11
答案很简单:你不能。默认的Android标签页将始终留下一些空白空间以容纳图片。 但是,您可以创建自己的选项卡来弥补默认选项卡中的“限制”。这里有一个非常好的教程,可以创建自定义选项卡。 http://joshclemm.com/blog/?p=136 祝好运, Arkde

太好了!运行得很好 - 必须在XML中更改选项卡方向,并阅读评论才能弄清如何添加意图。 感谢Arkde! - GideonKain

2

在XML中更改TabWidget的layout_height和gravity对我起作用。标签文本没有居中显示,仍然沿着底部对齐。

<TabWidget android:id="@android:id/tabs"
  android:layout_width="fill_parent" android:layout_height="40dp"
  android:gravity="bottom" />

1

从布局中更改您的TabHost大小,并仅为显示选项卡标题编写以下代码片段

tabhost=getTabHost();


intent = new Intent(this,MainActivity.class);
spec1 = tabhost.newTabSpec("").setIndicator("main_tab");
spec1.setContent(intent);
tabhost.addTab(spec1);

intent = new Intent(this,xyz.class);
spec2 = tabhost.newTabSpec("").setIndicator("first_tab");
spec2.setContent(intent);
tabhost.addTab(spec2);

我这样做了,所以它出现为一个没有图标和标题的大灰色框 - Arkde的解决方案似乎是正确的解决方法。 - GideonKain

0
// Center text displayed on a first tab
View view = _tabHost.getTabWidget().getChildAt(0);
if (view != null) {
    // Hide icon
    View tabImage = view.findViewById(android.R.id.icon);
    if (tabImage != null) {
        tabImage.setVisibility(View.GONE);
    }
    // Find text
    TextView tabTitle = (TextView) view.findViewById(android.R.id.title);
    if (tabTitle != null) {
        // Change text gravity
        tabTitle.setGravity(Gravity.CENTER);
        // Remove text view from it's parent and re-add back to reset layout parameters
        ViewGroup parent = (ViewGroup) tabTitle.getParent();
        parent.removeView(tabTitle);
        parent.addView(tabTitle);
        // New default layout parameters will have height set to WRAP_CONTENT, change it to MATCH_PARENT
        ViewGroup.LayoutParams params = tabTitle.getLayoutParams();
        params.height = ViewGroup.LayoutParams.MATCH_PARENT;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接