package com.example.ali.tabhostkullanm; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TabHost; public class MainActivity extends AppCompatActivity { TabHost tab; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tab=(TabHost)findViewById(R.id.tab); tab.setup(); TabHost.TabSpec s1=tab.newTabSpec("Sayfa-1");//yeni segmeler olusturuluyor TabHost.TabSpec s2=tab.newTabSpec("Sayfa-2"); TabHost.TabSpec s3=tab.newTabSpec("Sayfa-3"); s1.setContent(R.id.tab1);//her tabın Linearlayoutu ayarlanıyor s2.setContent(R.id.tab2); s3.setContent(R.id.tab3); s1.setIndicator("Sayfa-1");//gösterge ayarlanıyor s2.setIndicator("Sayfa-2"); s3.setIndicator("Sayfa-2"); tab.addTab(s1);//sayfalar tabhosta ekleniyor tab.addTab(s2); tab.addTab(s3); } }
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TabHost android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab" > <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"> <!-- 1.Sayfa layout'u--> </LinearLayout> <LinearLayout android:id="@+id/tab2" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 2.Sayfa layout'u--> </LinearLayout> <LinearLayout android:id="@+id/tab3" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" /> <!-- 3.Sayfa layout'u--> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost> </android.support.constraint.ConstraintLayout>