在ActionBar.Tab更改标签宽度 [英] Change tab width in ActionBar.Tab
问题描述
我要创建有标签导航的应用程序。我显示图标代替文本。
I'm creating an app with Tab navigation. I display icons instead of text.
我希望标签只包住的形象,所以我不会有滚动到达所有的标签。
I want the tabs to only wrap the image, so I won't have to scroll to reach all the tabs.
我如何创建标签,只需充满屏幕?
How can I create tabs that just fills the screen?
这是怎样的标签目前看:
This is how the tabs currently look:
您可以看到我需要滚动......还有一种卡是看不见的这张截图。
You can see I need to scroll... There is another tab that is unseen in this screenshot.
这是我的$ C $下创建标签:
This is my code for creating the tabs:
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
actionBar.addTab(
actionBar.newTab()
.setIcon(R.drawable.tabs_bar_add_item)
.setContentDescription(TAB_ADD_ITEM)
.setTabListener(this)
.setCustomView(R.layout.add_item_tab)
);
actionBar.addTab(
actionBar.newTab()
.setIcon(R.drawable.tabs_bar_shopping_list)
.setContentDescription(TAB_SHOW_LIST)
.setTabListener(this)
);
actionBar.addTab(
actionBar.newTab()
.setIcon(R.drawable.tabs_bar_map)
.setContentDescription(TAB_SHOW_MAP)
.setTabListener(this)
);
actionBar.addTab(
actionBar.newTab()
.setIcon(R.drawable.tabs_bar_specials)
.setContentDescription(TAB_SHOW_SPECIALS)
.setTabListener(this)
);
第一个 R.layout.add_item_tab
只是一个简单的ImageView的。我尝试了这种方式...
The first R.layout.add_item_tab
is just a simple imageView. I tried it that way...
任何想法?
推荐答案
最后,我已经找到了原因,也许它可以帮助别人......
Finally I've found the cause, maybe it can help someone...
该错误是我的图标都太大,不适合到一个单一的标签。即使他们按比例缩小的,他们的计算大小的标签宽度为原来的大小。
The error was that my icons were too big to fit into a single tab. Even though they were scaled-down, their calculation size for the Tab width was the original size.
我的解决办法是换用ImageView的这些图像,并设置其宽度
MY solution was to wrap those images with ImageView, and set its width to
ImageWidth等=屏幕宽度/ NumOfTabs - 2 * 16DP(转换为像素)
ImageWidth = ScreenWidth/NumOfTabs - 2*16dp(Converted to pixels)
额外的16 * 2是用于填充
The extra 16*2 is for the padding.
在另外,可以禁用填充
这篇关于在ActionBar.Tab更改标签宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!