Android Tabs without icons

2019-06-17 05:20发布

I've setup my tabs as follows:

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

And now I have a tab that has no icon, only a title, but it just leaves an icon-sized blank space with the Title at the bottom - I tried adjusting the layout_height in the xml, but then the text is gone because it is rendered below the cutoff point.

How can I change the size of a tab, and have the title displayed without an icon?

4条回答
forever°为你锁心
2楼-- · 2019-06-17 05:50

Changing the TabWidget layout_height and gravity in xml worked for me. The text does not center in the tab but is aligned along the bottom just like before.

<TabWidget android:id="@android:id/tabs"
  android:layout_width="fill_parent" android:layout_height="40dp"
  android:gravity="bottom" />
查看更多
时光不老,我们不散
3楼-- · 2019-06-17 05:58

The answer is simple: You can't. The default android tab will always leave some blank space for the image. But you can create your own tabs to compensate that "restriction" in the default tab. Here is a very good tutorial to create custom tabs.

http://joshclemm.com/blog/?p=136

Good luck, Arkde

查看更多
做个烂人
4楼-- · 2019-06-17 05:59
// 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;
    }
}
查看更多
甜甜的少女心
5楼-- · 2019-06-17 06:02

Change your tabhost size from layout and for display only Tab Tile write code as per below code snippets

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);
查看更多
登录 后发表回答