Android-015-TabHostの例

11830 ワード

Android-015-TabHost例-2020-4-19
新規プロジェクト(Empty Activity)
MainActivity.java
package com.example.index_school_00;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TabHost;


public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        //   TabHost  
        tab.setup();
        // TabHost    ,    :  /  /     
        tab.addTab(tab.newTabSpec("tab1").setIndicator("    " , null).setContent(R.id.tab1));
        tab.addTab(tab.newTabSpec("tab2").setIndicator("    " , null).setContent(R.id.tab2));

    }
}

activity_main.xml
<TabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- TabWidget  id    -->
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </TabWidget>

        <!-- FrameLayout  ,id    -->
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@android:id/tabs">
            <!--    tab    -->
            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="   tab   " />
            </LinearLayout>
            <!--    tab    -->
            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="   tab   " />
            </LinearLayout>

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