Android:TabHost 在运行时不可见

标签 android android-tabhost

我见过两个这样的问题,但他们没有答案。

有人说:you should build TabHost at Runtime.

我认为这不是答案。那么XML设计的目的是什么,如果它应该从代码中初始化。

所以,我的问题还是一样:

这是一些TabHost的XML设计

<?xml version="1.0" encoding="utf-8"?>
<TabHost
    android:id="@+id/tabHost"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/tab0"
                android:orientation="vertical"
                android:background="@color/controlDarkDark"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent"
                android:layout_gravity="center"
                android:visibility="visible">
                <Button
                    android:id="@+id/button1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button on Tab0" />
            </LinearLayout>>

            <LinearLayout
                android:id="@+id/tab1"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <Button
                    android:id="@+id/button2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button on Tab1" />
            </LinearLayout>

        </FrameLayout>
    </LinearLayout>
</TabHost>

我简化了结构,但仍然在运行时得到屏幕(见图片)。以及

的查询

TabHost tabHost = (TabHost)findViewById(R.id.tabHost);

返回空值。 enter image description here

最佳答案

试试这个。

在 xml 的根目录中。

添加LinearLayout

然后添加你的代码。

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

    TabHost mTabHost = (TabHost) findViewById(R.id.tabs);
    mTabHost.setup();
    mTabHost.addTab(mTabHost.newTabSpec("tab0").setIndicator("title1", null).setContent(R.id.button1));
    mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("title2", null).setContent(R.id.button2));

}

注意

如果您的 xml 代码运行良好,则说明您编辑了正确的 xml 代码。

Android Studio 完全基于您编写的 XML 的粗略呈现。

Android Studio 预览界面与实际界面并不完全等同,差异来自于Android Studio 对实际界面效果的复制偏差。

而且不同的Theme表现出不同的观点。

但在 Activity 中,您必须编写正确的代码并在其中显示。

关于Android:TabHost 在运行时不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45863577/

相关文章:

java - 不幸的是我的第一个应用程序在模拟器中停止了

java - 连接在远程 IP 上被拒绝,但在本地 IP 上被接受

android - 在 Android 中启动一个 AnimationDrawable

android - 如何让 Andorid 的 ActionBar/TabWidget LinearLayout 包裹它的 child

Android TabHost 导致运行时错误(NullPointerException)

android - 不想在特定选项卡单击时显示选项卡布局

android - 如何降级Google Play中的api?

android - 我在运行应用程序时收到 SQLCipher 错误

android - 在android中检测面部地标点

Android - 带有过渡动画的 TabActivity