【问题标题】:Conditional use of tabbed activity有条件地使用选项卡式活动
【发布时间】:2018-07-16 12:10:14
【问题描述】:

我正在尝试创建一个以具有“自定义”布局的活动 A 开头的应用程序。从该活动中,可以使用 3 个选项卡进入活动 B。

我成功地使用标签创建活动。问题是只有当该活动也是启动器活动时才会显示选项卡。如果不是,则不会显示选项卡。

第一次我尝试使用 TabLayout 创建选项卡。现在我尝试使用 TabHost 创建标签。

有人可以帮忙吗?

【问题讨论】:

  • 你能发一个代码sn-p吗?
  • 您对代码的哪一部分感兴趣?

标签: android tabs launcher


【解决方案1】:

在“创建”选项卡的主要活动中的 onCreate 方法。

    public class GlavnaStran extends TabActivity {

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

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

    TabHost.TabSpec tab1 = tabHost.newTabSpec("Search");
    TabHost.TabSpec tab2 = tabHost.newTabSpec("Lyric");
    TabHost.TabSpec tab3 = tabHost.newTabSpec("Playlist");

    //tab1.setIndicator("Tab1");
    tab1.setIndicator("Search");
    tab1.setContent(new Intent(this, SearchTab.class));

    //tab2.setIndicator("Tab2");
    tab2.setIndicator("Lyric");
    tab2.setContent(new Intent(this, LyricTab.class));

    //tab3.setIndicator("Tab3");
    tab3.setIndicator("Playlist");
    tab3.setContent(new Intent(this, PlaylistTab.class));

    tabHost.addTab(tab1);
    tabHost.addTab(tab2);
    tabHost.addTab(tab3);
}

}

对于每个选项卡,我都创建了单独的活动,如下所示:

    public class SearchTab extends Activity {

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

}

通过在设计视图中导入 TabHost 容器来创建项目的 xml 部分。

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.admin.besedilatakt_v5.GlavnaStran">

<TabHost
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@android:id/tabhost">

    <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" />

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

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">


            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">


            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">


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

在标签活动的 XML 文件中,除了 TextView 小部件显示一些随机文本之外没有什么特别的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多