在 ActionBar.Tab 中更改选项卡宽度 [英] Change tab width in ActionBar.Tab

查看:27
本文介绍了在 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.

这是我创建标签的代码:

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...

错误是我的图标太大而无法放入单个选项卡中.尽管它们是按比例缩小的,但它们对 Tab 宽度的计算大小是原始大小.

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 = ScreenWidth/NumOfTabs - 2*16dp(转换为像素)

ImageWidth = ScreenWidth/NumOfTabs - 2*16dp(Converted to pixels)

额外的 16*2 用于填充.

The extra 16*2 is for the padding.

此外,可以禁用填充.

这篇关于在 ActionBar.Tab 中更改选项卡宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆