TabHost - tabulações

Top  Previous  Next

Projeto

 

Observação: o main.xml não foi usado!

Tela

 

janela3.xml

 
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  android:orientation="vertical" >

 

 

  <TextView

      android:id="@+id/textView1"

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:text="Terceira"

      android:textAppearance="?android:attr/textAppearanceLarge" />

 

</LinearLayout>

 

Tab1.java

 

package com.abas;

 

import android.app.Activity;

import android.graphics.Color;

import android.os.Bundle;

import android.view.ViewGroup.LayoutParams;

import android.widget.LinearLayout;

import android.widget.TextView;

 

public class Tab1 extends Activity {

 

 @Override

 public void onCreate(Bundle icicle) {

         super.onCreate(icicle);

 

         // Cria o layout

         LinearLayout layout = new LinearLayout(this);

         layout.setOrientation(LinearLayout.VERTICAL);

         layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

         layout.setPadding(10, 10, 10, 10);// pixels

         layout.setBackgroundColor(Color.GRAY);

 

         TextView nome = new TextView(this);

         nome.setText("Texto da Tab 1");

         nome.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

         layout.addView(nome);

         setContentView(layout);

 }

}

 

Tab3.java

 

package com.abas;

 

import android.app.Activity;

import android.os.Bundle;

 

public class Tab3 extends Activity {

 

 @Override

 protected void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);

         setContentView(R.layout.janela3);

 }

}

 

Principal - AbasActivity.java

 

package com.abas;

 

import android.app.TabActivity;

import android.content.Intent;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.TabHost;

import android.widget.TabHost.OnTabChangeListener;

import android.widget.TabHost.TabContentFactory;

import android.widget.TabHost.TabSpec;

import android.widget.TextView;

 

public class AbasActivity extends TabActivity implements OnTabChangeListener, TabContentFactory {

 private static final String CATEGORIA = "livro";

 

 @Override

 public void onCreate(Bundle icicle) {

         super.onCreate(icicle);

 

         // getTabHost é da TabActivity

         TabHost tabhost = getTabHost();

         tabhost.setOnTabChangedListener(this);

         

         // tab1 - será aberta com intent

         TabSpec tab1 = tabhost.newTabSpec("Primeira");

         tab1.setIndicator("Primeira", getResources().getDrawable(R.drawable.smile1));

         tab1.setContent(new Intent(this, Tab1.class));

         tabhost.addTab(tab1);

         

         // tab2 = sera aberta com TabContentFactory

         TabSpec tab2 = tabhost.newTabSpec("Segunda");

         tab2.setIndicator("Segunda", getResources().getDrawable(R.drawable.smile2));

         tab2.setContent(this);

         tabhost.addTab(tab2);

 

         // tab3 = xml

         TabSpec tab3 = tabhost.newTabSpec("Terceira");

         tab3.setIndicator("Terceira", getResources().getDrawable(R.drawable.louco));

         tab3.setContent(new Intent(this, Tab3.class));

         tabhost.addTab(tab3);

 }

 

 public View createTabContent(String arg0) {

         TextView tv = new TextView(this);

         tv.setText("Fabrica");

         return tv;

 }

 

 public void onTabChanged(String tabidx) {

         Log.i("Programa", "Trocou aba: " + tabidx);

 }

}