【问题标题】:Android TabHost Layout QuestionAndroid TabHost 布局问题
【发布时间】:2011-01-11 19:25:01
【问题描述】:

这是我的第一个用于 Android 的选项卡式应用程序。我浏览了 androids 网站上的“HelloTabWidget”应用程序,但我不知道如何向选项卡添加内容。 FrameLayout 将东西堆叠在左上角(根据我的阅读)。我添加了几个 textview 和一个 imageView,但它只显示添加的最后一项。有没有办法使用线性布局而不是框架布局?如果没有,如何在选项卡中放置多个视图?我所做的与示例唯一不同的是我添加了第 4 个选项卡。在“活动”选项卡之一中,我插入了以下代码以尝试显示多个项目:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    

        TextView textview = new TextView(this);
        textview.setText("This is the About tab");
        setContentView(textview);

        TextView textview2 = new TextView(this);
        textview2.setText("About Test");
        setContentView(textview2);

        ImageView imgView = new ImageView(this);
        imgView.setImageDrawable(getResources().getDrawable(R.drawable.header));
        setContentView(imgView);
    }

这是我所遵循的示例的链接: http://developer.android.com/resources/tutorials/views/hello-tabwidget.html

【问题讨论】:

    标签: android android-tabhost android-linearlayout android-framelayout


    【解决方案1】:

    您的布局 xml 文件是什么?我使用它,我可以在每个选项卡中堆叠几个 textview。选项卡活动:

    <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">
        <ScrollView android:layout_width="fill_parent" 
            android:layout_height="wrap_content" >
            <LinearLayout android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
                android:orientation="vertical" >
                <TabWidget android:id="@android:id/tabs"
                    android:layout_width="fill_parent" 
                    android:layout_height="wrap_content" />
                <FrameLayout android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent" 
                    android:layout_height="fill_parent" />
            </LinearLayout>
        </ScrollView>
    </TabHost>
    

    标签内的活动:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <TextView android:id="@+id/textOne" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView android:id="@+id/textTwo" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    <LinearLayout>
    

    我自己也是新手,所以可能有更好的方法。不过,这对我有用。

    【讨论】:

    • 感谢您的帮助。但我没有多个布局文件。在示例中,它只有 main.xml,并且您使用 Java 填充每个选项卡。我将如何为每个选项卡添加另一个布局文件,同时保持选项卡在视图中?
    • 以同样的方式。使用选项卡的第一个布局,并在每个选项卡中设置不同的活动。在这些活动中,您可以通过代码创建布局,就像您在提供的 sn-p 中所做的那样。唯一的注意事项(我可能错了),你应该只使用一次 setContenteView() 。创建一个 LinearLayout 或其他任何东西并将 TextViews 放入其中。然后将其设置为内容视图。
    • 我想我不明白你是怎么做的。如果您使用 setContentView() 并发送一个新布局,它将位于具有选项卡的布局之上,对吗?如果你有这个运行,你能告诉我更多的代码吗?也许是活动代码之一?再次感谢。
    • 对不起,我误读了您的评论。要向选项卡添加活动,请参阅 Cristian 的代码。要创建布局文件,您可以在 res/layout 文件夹中创建一个新的 activity1.xml。从您的内部活动中执行onCreate(){super.onCreate(savedInstanceState);setContentView(R.layout.activity1);} 并在其中放置活动遵循的任何逻辑。当然,每个选项卡都有不同的活动。布局文件,即取决于您是否在每个选项卡中重复使用相同的布局。不过,如何创建新的布局和活动是非常基本的概念。也许您也应该回到文档。
    • 对于我的误解,我深表歉意。我认为如果我使用 setContentView(R.layout.newxmllayout) 它将覆盖选项卡所在的布局,并隐藏选项卡。我刚回到我的电脑上,无论如何都试了一下,它工作正常。感谢您的帮助。
    【解决方案2】:

    您没有仔细阅读示例。看看第 6 点;你会看到类似的东西:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab
    
        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, ArtistsActivity.class);
    
        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("artists").setIndicator("Artists",
                          res.getDrawable(R.drawable.ic_tab_artists))
                      .setContent(intent);
        tabHost.addTab(spec);
    
        // Do the same for the other tabs
        intent = new Intent().setClass(this, AlbumsActivity.class);
        spec = tabHost.newTabSpec("albums").setIndicator("Albums",
                          res.getDrawable(R.drawable.ic_tab_albums))
                      .setContent(intent);
        tabHost.addTab(spec);
    
        intent = new Intent().setClass(this, SongsActivity.class);
        spec = tabHost.newTabSpec("songs").setIndicator("Songs",
                          res.getDrawable(R.drawable.ic_tab_songs))
                      .setContent(intent);
        tabHost.addTab(spec);
    
        tabHost.setCurrentTab(2);
    }
    

    这就是您在TabActivity 的 onCreate 中添加的内容。如您所见,它有 3 个活动。您正在做的是使用 JUST ONE 活动并设置内容视图 3 次,这显然是错误的。

    那么...如何使它工作?首先,再次阅读教程。其次,为您要显示的每个选项卡创建一个活动。并使用上面的模型将这些活动添加到您的TabHost

    【讨论】:

    • 如果我不清楚,我很抱歉。这就是我在主要课程中的内容。例如,我发布的代码位于 ArtistsActivity 类中。在示例中,他们只使用了一个 textView,我在其中添加了另一个 textView 和一个 ImageView。
    • 在这种情况下,专注于该活动并阅读LinearLayout教程developer.android.com/resources/tutorials/views/…你就快到了!
    • 我知道如何使用 LinearLayout(至少在 xml 中使用它)。但是,由于我使用的是选项卡主机并且只有 1 个主 xml 文件,因此我必须将内容添加到 Java 中的每个选项卡(就像我原始帖子中的代码一样)。使用我发布的代码,我试图将 3 个项目添加到单个选项卡中,问题是我添加的最后一件事可以看到。在我的主要 XML 文件中,我有 。我不知道该怎么解释。
    • 基本上,如果我想在一个选项卡中添加 2 个文本视图,我该怎么做(假设我遵循了 HelloTabWidget 示例)?您不能只是简单地将其添加到 XML 文件中,因为这会将 textView 添加到所有选项卡中,对吧?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多