我已经按照以下方式设置了我的选项卡:
spec = tabHost.newTabSpec("tab1").setIndicator("Tab 1").setContent(intent);
tabHost.addTab(spec);
现在我有一个没有图标的选项卡,只有一个标题,但它会在底部留下一个图标大小的空白空间 - 我尝试调整xml中的layout_height,但那么文字就消失了,因为它被渲染在截断点之下。
我如何改变选项卡的大小,并显示没有图标的标题?
我已经按照以下方式设置了我的选项卡:
spec = tabHost.newTabSpec("tab1").setIndicator("Tab 1").setContent(intent);
tabHost.addTab(spec);
现在我有一个没有图标的选项卡,只有一个标题,但它会在底部留下一个图标大小的空白空间 - 我尝试调整xml中的layout_height,但那么文字就消失了,因为它被渲染在截断点之下。
我如何改变选项卡的大小,并显示没有图标的标题?
在XML中更改TabWidget的layout_height和gravity对我起作用。标签文本没有居中显示,仍然沿着底部对齐。
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent" android:layout_height="40dp"
android:gravity="bottom" />
从布局中更改您的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);
// 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;
}
}