微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么Tab主机不在android中显示图标?

这是设置tabhost的代码,但是有两个问题

>如果文本太长,文本将转到下一行,我可以减少
尺寸并强制它到单线?
>所有图标都不显示,即使我确定图像src是正确的

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentTabHost tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);

        tabHost.setup(this,getSupportFragmentManager(),R.id.realtabcontent);
        tabHost.addTab(tabHost.newTabSpec("restaurant").setIndicator("Restaurant",getResources().getDrawable(R.drawable.food)),PlaceList.class,null);
        tabHost.addTab(tabHost.newTabSpec("attraction").setIndicator("Attraction",getResources().getDrawable(R.drawable.view)),null);
        tabHost.addTab(tabHost.newTabSpec("map").setIndicator("Map",getResources().getDrawable(R.drawable.map)),Map.class,null);
        tabHost.addTab(tabHost.newTabSpec("planner").setIndicator("Planner",getResources().getDrawable(R.drawable.plan)),Planner.class,null);
    }
 }

解决方法

调用TabSpec.setIndicator时,你在 will only be visible if the label is null or empty.中传递的Drawable只要确保TextView被限制为单行就可以了,你可以遍历TabWidget.getTabCount,然后调用TabWidget.getChildTabViewAt和View.findViewById来获取用于的TextView设置标题.之后,只需调用TextView.setSingleLine.

final TabWidget tabWidget = tabHost.getTabWidget();
    for (int i = 0; i < tabWidget.getTabCount(); i++) {
        final View tab = tabWidget.getChildTabViewAt(i);
        final TextView title = (TextView) tab.findViewById(android.R.id.title);
        title.setSingleLine();
    }

或者,您可以通过为Widget.TabWidget创建样式来扩展自己的选项卡布局.

<style name="Your.TabWidget" parent="@android:style/Widget.TabWidget">
    <item name="android:tabLayout">@layout/your_tab_layout</item>
</style>

在您的父主题中为android创建一个新项:tabWidgetStyle来应用它.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐