Android:TabHost在运行时不可见

Android:TabHost在运行时不可见,android,android-tabhost,Android,Android Tabhost,我见过两个这样的问题,但都没有答案 有人说: 我认为这不是答案。那么,如果应该从代码初始化XML,那么XML设计的目的是什么呢 所以,我的问题还是一样的: 这是某个TabHost的XML设计 <?xml version="1.0" encoding="utf-8"?> <TabHost android:id="@+id/tabHost" xmlns:android="http://schemas.android.com/apk/res/android"

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

有人说:

我认为这不是答案。那么,如果应该从代码初始化XML,那么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>
试试这个

在xml的根目录中

添加
线性布局

然后添加代码

@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对实际界面效果的复制偏差

不同的主题表现出不同的观点


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

只是为了澄清:tabhost没有显示,因为他没有向其中添加选项卡。您的答案是正确的,但没有解释问题所在。您需要在TabHost上调用setup()、newTabSpec()和addTab()方法来设置选项卡。您可以查看此示例。祝您好运~@KeLiuyue,谢谢您的帮助。是的,你是对的,当在运行时使用控件充气时它会工作,但我仍然不明白为什么是XML?