Android tabhost tabwidget kullanımı Eclipse

Arif Ceylan/ Aralık 10, 2013/ Android Programlama/ 2 comments

Tabornegi adlı yeni bir uygulama oluşturuyoruz.

Layout.xml den Hello World textviewini silip Tabhost bileşeninden ekliyoruz. Tabhostumuz relative layout içerisinde olmayacak. O bölümü kodlardan kaldıracağız. Yalnız xmlns parametrelerini de tabhosta eklemek gerekiyor.

Seçili olan bölümü TabHost içerisine alıyorum ve relativeLayout etiketinin başlangıç bölümünü ve bitiş bölümünü siliyorum.

Daha sonra Graphical Layouta gelip tabların içerisine bileşenler sürüklüyorum. Yalnız orta kısımda bileşenler tab içerisinde gözükmediğinden bunu ya kod kısmında yapmak gerekiyor ya da şekildeki gibi sürüklemek gerekiyor.

Tab1, tab2, tab3 içerisine textviewler ve edittext ekledim. Text özelliklerini değiştirebilirsiniz.

Uygulamanın kodları:

activity_main.xml

 

    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

        <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/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <EditText
                        android:id="@+id/editText1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:ems="10"
                        android:text="Burası tab1" >

                        <requestFocus />
                    </EditText>

                </LinearLayout>

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

                    <TextView
                        android:id="@+id/textView1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Burası tab2" />

                </LinearLayout>

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

                    <TextView
                        android:id="@+id/textView2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Burası tab3 tür"
                        android:textAppearance="?android:attr/textAppearanceLarge" />

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

 

Main activity de ;

package com.ceylan.tabornegi;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost=(TabHost)findViewById(android.R.id.tabhost);
tabHost.setup();
//tabhostumu tanımlayıp ayarlamaları yapıyorum.
//aşağıdaki bölümde tabhosta 3 ayrı tab eklenmektedir.
TabSpec tabSpec1=tabHost.newTabSpec("tab1");
tabSpec1.setContent(R.id.tab1);
tabSpec1.setIndicator("Birinci tab",null);
tabHost.addTab(tabSpec1);
TabSpec tabSpec2=tabHost.newTabSpec("tab2");
tabSpec2.setContent(R.id.tab2);
tabSpec2.setIndicator("İkinci tab",null);
tabHost.addTab(tabSpec2);
TabSpec tabSpec3=tabHost.newTabSpec("tab3");
tabSpec3.setContent(R.id.tab3);
tabSpec3.setIndicator("Üçüncü Tab",null);
tabHost.addTab(tabSpec3);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Share this Post

2 Comments

  1. teşekkür ederim

  2. Öncelikle çok teşekkür ederim kendi uygulamam da tabhostlar kullanıyorum her tab da farklı tablolardan veri göstermek istiyorum acaba veritabanı kodlarımı onselected tab içerisine mi almam gerekiyor hocam bununla ılgılı bır çalışmanız varmı yada teşekkür ederim

Leave a Comment

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
*
*