2012-10-22 29 views
-1

我正在一個項目上,我需要通過從頂部選擇不同的標籤在單個窗口中顯示不同的視圖。顯示不同的活動使用頂部的標籤

我已經google了很多這個話題,但我不知道這個視圖使用了什麼確切的術語,所以很難找到它。 我的工作就是這樣的一個觀點,

Tab Activities

我已經通過幾個例子去像this

this

,但我無法得到所需的答案。

任何有用的鏈接,可以指導我實現這個視圖將是有益的。

+0

怎麼樣actionBar或actionBarSherlock? – Krishnabhadra

+1

[試過這個?](http://www.androidhive.info/2011/08/android-tab-layout-tutorial/)和[this](http://www.powenko.com/en/?p = 2121) – Praveenkumar

+0

@Krishnabhadra那是什麼。 –

回答

1

在Android中有很多類型的TabHost。您可以使用默認的一個,或者您可以使用它自定義它。

只要看看this的例子。它會爲您提供默認的機器人tabHost一個在下面的代碼看看,這將提供一個TabHost按您的需求 -

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    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"> 
     <TabWidget android:id="@android:id/tabs" 
      android:layout_width="fill_parent" android:layout_height="wrap_content" /> 
     <FrameLayout android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" android:layout_height="fill_parent"> 
     </FrameLayout> 
    </LinearLayout> 
</TabHost> 

而且,看看here也。

2

tab_host.xml

<?xml version="1.0" encoding="utf-8"?> 
<TabHost 
android:id="@android:id/tabhost" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="#0F5EC6" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/back" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="5dp" 
      android:text="Back" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="80dp" 
      android:gravity="center" 
      android:text="ThirdEye" 
      android:textSize="20dp" 
      android:textStyle="bold" /> 

     <Button 
      android:id="@+id/logout" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="30dp" 
      android:gravity="center" 
      android:text="Logout" /> 
    </LinearLayout> 

    <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="0dip" 
     android:layout_weight="1" /> 
</LinearLayout> 

tabactivity

public class PostTabActivity extends TabActivity implements TabHost.OnTabChangeListener { 
private TabHost mTabHost; 
Button back,logout; 
private String sessid,Name; 
double lati,longi; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    final Intent intent = getIntent(); 
    Bundle extras = getIntent().getExtras(); 
    if (extras != null) { 
     sessid = extras.getString("sid"); 
     lati = extras.getDouble("EXTRA_latitude"); 
     longi = extras.getDouble("EXTRA_longitude"); 
     Name= extras.getString("NICK_NAME"); 
     System.out.println("........................"+lati+".................."+longi); 
    } 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.tab_host); 
    logout = (Button) findViewById(R.id.logout); 
    logout.setOnTouchListener(new OnTouchListener() 
    { 

     public boolean onTouch(View v, MotionEvent event) { 
      switch(event.getAction()) 
      { 
      case MotionEvent.ACTION_DOWN: 
       break; 
      case MotionEvent.ACTION_UP: 
      new Logout(PostTabActivity.this,sessid); 
       //finish(); 
       break; 
      } 
      return true; 
     } 
    }); 
    back= (Button) findViewById(R.id.back); 
    back.setOnTouchListener(new OnTouchListener() 
    { 

     public boolean onTouch(View v, MotionEvent event) { 
      switch(event.getAction()) 
      { 
      case MotionEvent.ACTION_DOWN: 
       break; 
      case MotionEvent.ACTION_UP: 
       Intent myIntent = new Intent(PostTabActivity.this, GetPost.class); 
       myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       myIntent.putExtra("EXTRA_SESSION_ID", sessid); 
       myIntent.putExtra("EXTRA_latitude",lati); 
       myIntent.putExtra("EXTRA_longitude", longi); 
       myIntent.putExtra("NICK_NAME", Name); 
       startActivity(myIntent); 
       finish(); 
       break; 
      } 
      return true; 
     } 


    } 
     ); 


    mTabHost = getTabHost(); 
    mTabHost.setOnTabChangedListener(this); 

    /*Tab One */ 

    Intent mIntent= new Intent("proto.thirdeye.PostText"); 
    mIntent.setClass(this, PostText.class); 
    mIntent.putExtra("sid",sessid); 
    mIntent.putExtra("EXTRA_latitude",lati); 
    mIntent.putExtra("EXTRA_longitude", longi); 
    mIntent.putExtra("NICK_NAME", Name); 

    mTabHost.addTab(mTabHost.newTabSpec("text1") 
      .setIndicator("Text", 
        getResources().getDrawable(R.drawable.image)) 
      .setContent(mIntent)); 

    /*Tab Two */ 

    Intent mIntent1 = new Intent("proto.thirdeye.PostImage"); 
    mIntent1.setClass(this, PostImage.class); 
    mIntent1.putExtra("sid",sessid); 
    mIntent1.putExtra("EXTRA_latitude",lati); 
    mIntent1.putExtra("EXTRA_longitude", longi); 
    mIntent1.putExtra("NICK_NAME", Name); 
    mTabHost.addTab(mTabHost.newTabSpec("image1") 
      .setIndicator("Image", 
        getResources().getDrawable(R.drawable.image)) 
      .setContent(mIntent1)); 


    /*Tab Three */ 

    Intent mIntent2 = new Intent("proto.thirdeye.PostVideo"); 
    mIntent2.setClass(this, PostVideo.class); 
    mIntent2.putExtra("sid",sessid); 
    mIntent2.putExtra("EXTRA_latitude",lati); 
    mIntent2.putExtra("EXTRA_longitude", longi); 
    mIntent2.putExtra("NICK_NAME", Name); 
    mTabHost.addTab(mTabHost.newTabSpec("video") 
      .setIndicator("Video", 
        getResources().getDrawable(R.drawable.image)) 
      .setContent(mIntent2)); 

    mTabHost.setCurrentTab(0); 
} 
@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK) { 
     onBackPressed(); 

    } 

    return super.onKeyDown(keyCode, event); 
} 

public void onBackPressed() { 
    Intent myIntent = new Intent(PostTabActivity.this, GetPost.class); 
    myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    myIntent.putExtra("EXTRA_SESSION_ID", sessid); 
    myIntent.putExtra("EXTRA_latitude",lati); 
    myIntent.putExtra("EXTRA_longitude", longi); 
    myIntent.putExtra("NICK_NAME", Name); 
    startActivity(myIntent); 
    finish(); 
    return; 
} 
public void onTabChanged(String tabId) { 
    // TODO Auto-generated method stub 
    Log.i("Aru","     "+tabId); 
    Activity activity = getLocalActivityManager().getActivity(tabId); 
    if (activity != null) { 
     activity.onWindowFocusChanged(true); 
    } 
} 
@Override 
public void onSaveInstanceState(Bundle savedInstanceState) { 
    super.onSaveInstanceState(savedInstanceState); 
    // Save UI state changes to the savedInstanceState. 
    // This bundle will be passed to onCreate if the process is 
    // killed and restarted. 
    savedInstanceState.putString("sid",sessid); 
    savedInstanceState.putDouble("EXTRA_latitude",lati); 
    savedInstanceState.putDouble("EXTRA_longitude",longi); 
    savedInstanceState.putString("NICK_NAME", Name); 
    // etc. 
} 
@Override 
public void onRestoreInstanceState(Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 
    // Restore UI state from the savedInstanceState. 
    // This bundle has also been passed to onCreate. 
    sessid = savedInstanceState.getString("sid"); 
    lati = savedInstanceState.getDouble("EXTRA_latitude"); 
    longi = savedInstanceState.getDouble("EXTRA_longitude"); 
    Name = savedInstanceState.getString("NICK_NAME"); 
} 
@Override 
protected void onPause() { 
    // TODO Auto-generated method stub 
    super.onPause(); 
    //MyApplication.activityPaused(); 
} 
@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    //MyApplication.activityResumed(); 
} 
} 

您可以定義PostImage,PostVideo,PostText自己的佈局。根據您的需求定製這個。