【问题标题】:TabHost on Android 4.2 - Icons and Colours Not Showing on TabsAndroid 4.2 上的 TabHost - 选项卡上未显示图标和颜色
【发布时间】:2013-02-26 14:56:21
【问题描述】:

我们是 Android 开发的新手,如果这很明显,我们深表歉意。

我们正在使用选项卡式视图实施一项活动。我们遇到的问题是标签本身的图标和正确颜色在 Jelly Bean (Android 4.2) 上没有正确显示。但是,它们确实可以在早期的 API 级别(例如 Gingerbread)上正确显示。

请注意:我们最初使用已弃用的 TabActivity 类创建视图。但是,据我所知,这样做的“新”方式应该与旧方式没有什么不同?如果我错了,请纠正我。

这是包含选项卡主机布局的 xml:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/Black">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp">

        <include layout="@layout/logo_bar"/>

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"/>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
        </FrameLayout>
     </LinearLayout>
</TabHost>

这是相关(我希望)的活动代码:

public class MainTabActivity extends FragmentActivity implements TabHost.TabContentFactory
    private TabHost tabHost;
    // other instance variables ...

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        tabHost = (TabHost)findViewById(android.R.id.tabhost);
        tabHost.setup();

        TabSpec loginTabSpec = tabHost.newTabSpec(GlobalConstants.LOGIN_ACTIVITY);
        loginTabSpec.setIndicator("Settings", getResources().getDrawable(R.drawable.ic_action_settings_gear));
        loginTabSpec.setContent(this);

        TabSpec mainTabSpec = tabHost.newTabSpec(GlobalConstants.MAIN_ACTIVITY);
        mainTabSpec.setIndicator("Lone Worker", getResources().getDrawable(R.drawable.ic_action_settings_phone));
        mainTabSpec.setContent(this);

        tabHost.addTab(mainTabSpec);
        tabHost.addTab(loginTabSpec);
    }

这就是 Jelly Bean 上标签本身的样子(坏版本):

最后,它应该是这个样子(在姜饼上……好版本):

所以要重申:标签的背景颜色是黑色(当它通常是漂亮的蓝色时),虽然很难从糟糕的屏幕截图中分辨出来,但两个标签的图标都不见了在果冻豆上。我希望这个问题在这里得到了足够的概述。如果我遗漏了什么,请告诉我。提前谢谢!

【问题讨论】:

  • 你能告诉我或指导我...如何在 4.2 中的选项卡之间提供边距 .. 在 4.1 选项卡上的边距工作正常,当我在 4.2 中运行相同的项目时,选项卡之间的边距消失并且所有选项卡都占用相同的空间....如果您对此有一些解决方案,请指导我...谢谢
  • @NipunGogia 我不知道,我从来没有尝试在标签之间放置边距。您可能想就此发表您自己的问题,因为我的问题与此无关。
  • 随机投反对票有什么原因吗?

标签: android android-tabhost android-4.2-jelly-bean


【解决方案1】:

无论出于何种原因,在更高版本的 Android (>= ICS) 中,如果您正在使用,标签页只能有一个文本标签或一个图标

setIndicator(CharSequence label, Drawable icon)

我怀疑您看不到任何文本的原因是因为 ICS 中选项卡的默认文本颜色是黑色,所以它在您的黑色背景上不可见。

一种解决方案是创建自己的视图并使用

setIndicator(View view)

更多详情:Icon in Tab is not showing up

这里是http://ondrejcermak.info/en/programovani/custom-tabs-in-android-tutorial/

【讨论】:

    猜你喜欢
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多