如何在android中的自定义选项卡中使用intent?

时间:2013-11-20 10:48:02

标签: android android-intent android-tabhost android-tabs

大家好我正在为我的项目使用自定义标签。我的代码在http://pastie.org/8495005,我的main.xml在这里http://pastie.org/8495007我的自定义标签布局在这里http://pastie.org/8495010 我可以在单个选项卡中显示图像和文本。但是我不能通过意图将一个活动移动到另一个活动。我有四个选项卡和四个子类。我怎样才能做到这一点?任何人都可以帮我这么做吗?提前谢谢。

1 个答案:

答案 0 :(得分: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文件中。它对我来说很好用。一切顺利......