TabHost è un contenitore per visualizzare finestre a schede che consentono di passare da una visualizzazione all’altra.
È molto semplice da utilizzare,basta inserire all’ interno di un TabHost un LinearLayout per inserire i widget dei tab e un Frame layout per associare i contenuti da visualizzare:
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android“
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:gravity=”center”
android:orientation=”vertical”>
<TabHost
android:id=”@+id/tab_host”
android:layout_width=”match_parent”
android:layout_height=”match_parent”>
<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/tab_1″
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background=”#16d86b”>
<TextView
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:textSize=”30sp”
android:text=”Hallo 1″/>
</LinearLayout>
<LinearLayout
android:id=”@+id/tab_2″
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background=”#af20ad”>
<TextView
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:textSize=”30sp”
android:text=”Hallo 2″/>
</LinearLayout>
<LinearLayout
android:id=”@+id/tab_3″
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background=”#5b89ff”>
<TextView
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:textSize=”30sp”
android:text=”Hallo 3″/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
package com.example.tabHost.activities;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.example.tabHost.R;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
TabHost tabHost;
TabWidget tabWidget;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost=findViewById(R.id.tab_host);
tabHost.addTab(tabHost.newTabSpec(“”)
.setContent(R.id.tab_1)
.setIndicator(“Tab1”));
tabHost.addTab(tabHost.newTabSpec(“”)
.setContent(R.id.tab_2)
.setIndicator(“Tab2”));
tabHost.addTab(tabHost.newTabSpec(“”)
.setContent(R.id.tab_3)
.setIndicator(“Tab3”));
}
}