2013-11-20 16 views
1

大家好我爲我的項目使用自定義選項卡。我的代碼在這裏http://pastie.org/8495005和我的main.xml在這裏http://pastie.org/8495007我的自定義選項卡布局在這裏http://pastie.org/8495010 我可以在單個選項卡中顯示圖像和文本。但我無法按照意願將一項活動轉移到另一項活動。我有四個選項卡和四個子類。我怎樣才能做到這一點?任何機構都可以幫助我做到這一點?提前致謝。如何在Android中使用自定義選項卡中的意圖?

+1

ü應該去代替片段 –

+1

TabActivity已經廢棄了新版本......你應該使用片段與片段活動... – Piyush

回答

0

我在接下來的活動中犯了一些錯誤。我動態創建了我的內容。我沒有創建佈局,並將其設置爲setContentView(R.layout.train_layout);.現在它對我來說工作得很好,也很好。就這樣做。

CustomTabActivity.java

package com.joshclemm.android.tabs; 
import android.app.TabActivity; 
import android.content.Intent; 
import android.content.res.Resources; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.TabHost; 
import android.widget.TabHost.TabSpec; 
import android.widget.TextView; 
import android.widget.Toast; 

public class CustomTabActivity extends TabActivity 
{ 

private TabHost mTabHost; 
Intent intent1,intent2,intent3,intent4; 
TabHost.TabSpec spec1,spec2,spec3,spec4; 

private void setupTabHost() { 
    mTabHost = (TabHost) findViewById(android.R.id.tabhost); 
    mTabHost.setup(); 
} 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 


    setContentView(R.layout.main); 

    setupTabHost(); 
    Resources ressources = getResources(); 

    setTabs() ; 


    mTabHost.getTabWidget().getChildTabViewAt(0).setBackgroundDrawable(null); 
    mTabHost.getTabWidget().getChildTabViewAt(1).setBackgroundDrawable(null); 
    mTabHost.getTabWidget().getChildTabViewAt(2).setBackgroundDrawable(null); 
    mTabHost.getTabWidget().getChildTabViewAt(3).setBackgroundDrawable(null); 


} 




private void setTabs() 
    { 


     addTab("Tab1",R.drawable.tab1_selector,Activity1.class); 
     addTab("Tab2",R.drawable.tab2_selector,Activity2.class); 
     addTab("Tab3",R.drawable.tab3_selector,Activity3.class); 
     addTab("Tab4",R.drawable.tab4_selector,Activity4.class); 
    } 


private void addTab(String labelId, int drawableId, Class<?> c) 
    { 
    View view = LayoutInflater.from(getBaseContext()).inflate(R.layout.tabs_item, null); 
    TextView tv = (TextView) view.findViewById(R.id.tabsText); 
    ImageView img1 = (ImageView)view.findViewById(R.id.tabsImg); 
    tv.setText(labelId); 

    img1.setImageResource(drawableId); 

    TabHost tabHost = getTabHost(); 
    Intent intent = new Intent(this, c); 
    TabHost.TabSpec spec = tabHost.newTabSpec("" + labelId); 

    spec.setIndicator(view); 
    spec.setContent(intent); 
    tabHost.addTab(spec); 
    int i = tabHost.getCurrentTab(); 
    Toast.makeText(CustomTabActivity.this, "tab->" + i,Toast.LENGTH_SHORT).show(); 

    if(i==0) 
    { 
     Intent intentAndroid = new Intent().setClass(this, Activity1.class); 
     TabSpec tabSpecAndroid = tabHost 
      .newTabSpec("Android")    
      .setContent(intentAndroid); 
    } 
    else if(i==1) 
    { 
     Intent intentApple = new Intent().setClass(this, Activity2.class); 
     TabSpec tabSpecApple = tabHost 
      .newTabSpec("Apple")     
      .setContent(intentApple); 
    } 
    else if(i==2) 
    { 
     Intent intentWindows = new Intent().setClass(this, Activity3.class); 
     TabSpec tabSpecWindows = tabHost 
      .newTabSpec("Windows") 

      .setContent(intentWindows); 
    } 
    else if(i==3) 
    { 
     Intent intentBerry = new Intent().setClass(this, Activity4.class); 
     TabSpec tabSpecBerry = tabHost 
      .newTabSpec("Berry") 

      .setContent(intentBerry); 
    } 

} 
} 

Activity1.java

package com.joshclemm.android.tabs; 

import android.app.Activity; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.widget.TextView; 

public class Activity1 extends Activity 
{ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tab1_layout); 
     /* TextView tv = new TextView(this); 
    tv.setText("This is tab1...."); 
    tv.setTextColor(Color.parseColor("#00B589"));*/ 
} 
} 

重複它剩下的三個佈局。

main.xml中

<TabHost 
    android:id="@android:id/tabhost" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout android:orientation="vertical" 
     android:layout_width="fill_parent" android:layout_height="fill_parent"> 
     <View android:layout_width="fill_parent" android:layout_height="0.5dip" 
      android:background="#000" /> 
     <TabWidget android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="120dp" 
      android:layout_marginLeft="0dip" 
      android:layout_marginRight="0dip" 
       android:showDividers="none" 
       android:background="@drawable/tab_bg" 
     android:tabStripEnabled="false"/> 
     <View android:layout_width="fill_parent" android:layout_height="2dip" 
      android:background="#696969" /> 
     <View android:layout_width="fill_parent" android:layout_height="2dip" 
      android:background="#000" /> 
     <FrameLayout android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" android:layout_height="fill_parent" /> 
    </LinearLayout> 
</TabHost> 

tab_item.xml

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


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/tabsLayout" android:layout_width="fill_parent" 

android:layout_height="fill_parent" android:background="@drawable/tab_bg_selector" 
android:padding="10dip" android:gravity="center" android:orientation="vertical"> 

<ImageView 
    android:id="@+id/tabsImg" android:layout_width="80dp" 
    android:layout_height="80dp" 
    android:layout_gravity="center" 

    /> 


<TextView android:id="@+id/tabsText" 
    android:layout_width="fill_parent" 
    android:layout_height="20dp" 

    android:textSize="15sp" 
    android:gravity="center|bottom" 
    android:textColor="@drawable/tab_text_selector" /> 

train_layout.xml

<?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:orientation="vertical" > 



<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

    android:text="This is train tab......"/> 

重複其用於其他三種佈局是這樣的...不要忘記您的所有活動添加到您的manifest.xml文件。它對我來說工作得很好。所有最好的...

相關問題