【问题标题】:Android: TabHost is not visible at runtimeAndroid:TabHost 在运行时不可见
【发布时间】:2017-08-24 13:57:19
【问题描述】:

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

有人说: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);

返回空值。

【问题讨论】:

    标签: android android-tabhost


    【解决方案1】:

    试试这个。

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

    并且不同的主题显示不同的视图。

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

    【讨论】:

    • 只是为了澄清:标签主机没有显示,因为他没有向其中添加标签。您的回答是正确的,但请不要解释问题所在。
    • 需要在 TabHost 上调用 setup()、newTabSpec() 和 addTab() 方法来设置标签。
    • 你可以看看这个样本link。祝你好运~
    • @KeLiuyue,谢谢你的帮助。是的,你是对的,它在运行时使用控件膨胀时有效,但我仍然不明白为什么是 XML?
    猜你喜欢
    • 2021-10-28
    • 1970-01-01
    • 2018-03-12
    • 2013-08-22
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多