2

我正在我的应用程序中设计标签。我很困惑是将活动用于选项卡还是仅使用视图

哪一种是tablayout的更好方法

我很困惑 ..

我对标签有点陌生,还在学习 android 中的标签布局,所以我的问题可能有点天真,所以请耐心等待:)

4

3 回答 3

4

回答“选项卡布局中的活动或视图”的问题

这就是android教程的内容:

您可以通过以下两种方式之一实现您的选项卡内容:使用选项卡在同一活动中交换视图,或使用选项卡在完全独立的活动之间进行更改。您希望应用程序使用哪种方法取决于您的需求,但如果每个选项卡都提供不同的用户活动,那么为每个选项卡使用单独的活动可能是有意义的,这样您就可以更好地管理离散组中的应用程序,而不是不仅仅是一个庞大的应用程序和布局。 Android tablayout教程

我强烈建议您遵循本教程,然后尝试为您的应用程序实现您自己的版本。

这是一个代码示例以防万一;

public class TabHostExample extends TabActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.testtabs); 

    TabHost tabHost = getTabHost();
    TabHost.TabSpec spec;
    Intent content;

    spec = tabHost.newTabSpec("test1"); //set a title for the tab
    spec.setIndicator("test1"),
            getResources().getDrawable(R.drawable.ic_dialog_alert)); //set an icon for tab
    content = new Intent(this, ExampleActivityOne.class);
    spec.setContent(content);
    tabHost.addTab(spec);

    spec = tabHost.newTabSpec("test2")); //set a title for the tab
    spec.setIndicator("test2"),
            getResources().getDrawable(R.drawable.ic_dialog_info)); //set an icon for the tab
    content = new Intent(this, ExampleActivityTwo.class);
    spec.setContent(content);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
}

}

    <TabHost
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabhost">
    <LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    >   
        <TabWidget
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@android:id/tabs" 
        />
        <FrameLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:id="@android:id/tabcontent" 
        />
    </LinearLayout>
</TabHost>

教程中的一些额外信息:

框架布局是每个选项卡的活动内容所在的位置。

请注意,TabWidget 和 FrameLayout 元素分别具有 ID 选项卡和选项卡内容。必须使用这些名称,以便 TabHost 可以检索对它们中的每一个的引用。它期望正是这些名称。

因此,使用此代码,您可以对选项卡布局进行基本设置,接下来要做的是将活动附加到选项卡。您像往常一样使用活动,对于这些活动,您可以像往常一样扩展活动并在 onCreate/onResume 中构建布局

如果您需要更多信息或更多解释,请发表评论。我认为大多数代码都是不言自明的。

我刚刚注意到我的代码几乎与教程页面完全相同。我实际上在我的应用程序中使用了这段代码,活动名称不同等。它工作得很好。我推荐它。归功于 Android 团队。

于 2012-05-02T12:39:29.863 回答
1

你必须使用活动。每个选项卡都有活动。developer.android.com 上有很好的教程 在这里试试这个

于 2012-05-02T12:21:20.850 回答
1

尝试活动..寻找 TabActivity,这是在应用程序中处理 TABS 的最佳方式.. http://developer.android.com/reference/android/app/TabActivity.htmlhttp://developer.android.com/资源/教程/视图/hello-tabwidget.html

于 2012-05-02T12:24:55.863 回答