问题描述
|
我只想在TabLayout的标签中使用文本。不幸的是,默认情况下,“选项卡”非常大,文本很小。如果不使用
TabSpec.setIndicator(View)
,我将无法调整文本的大小或标签的高度。这将是非常不可取的,因为那样我会定义一个特定的布局,选择器,选定的图像,未选定的图像等...,因此我的标签不适合每个设备的外观。有自定义标签外观的便捷方法吗?
mTabHost.addTab(mTabHost.newTabSpec(\"system\").setIndicator(\"My first tab\").setContent(R.id.fileBrowserTabHostSystemList));
mTabHost.addTab(mTabHost.newTabSpec(\"recent\").setIndicator(\"My Second tab\").setContent(R.id.fileBrowserTabHostRecentList));
解决方法
我设法以一种很好的方式更改了标签的高度:
final int tabChildrenCount = tabWidget.getChildCount();
View currentTab;
for (int i = 0; i < tabChildrenCount; i++) {
currentTab= tabWidget.getChildAt(i);
currentTab.getLayoutParams().height = 32;
}
tabWidget.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
tabWidget.requestLayout();
我还尝试在标签中获取标题的高度并将其高度设置为:
currentTab.findViewById(android.R.id.title).getHeight();
// also tried with getMeasuredHeight()
但是它返回0,因为它在onCreate()方法中,并且该视图当时似乎不知道其大小。 LayoutParams.WRAP_CONTENT对制表符高度不利,因为它充当FILL_PARENT。
但是请注意,此解决方案仅假定选项卡的标题适合32像素。
仍然最安全的猜测是手动进行布局(如已经发布的http://joshclemm.com/blog/?p=136)。从Android存储库获取资源并自定义:)