2013-03-06 80 views
1

我有一个在屏幕底部有2个标签的屏幕。 一旦我运行该应用程序,自动第一个选项卡将被激活,并显示与该屏幕相关的活动(屏幕1)。然后,当用户点击第二个标签时,它会打开另一个活动(屏幕2)。标签活动第一个屏幕独立于标签

要求:我想在应用程序加载时显示屏幕0而不是显示屏幕1.此时,这两个选项卡应该处于非活动状态(未按下)。一旦用户点击任何标签,只有相应的屏幕应该打开(屏幕1和屏幕2)。

怎么办?

我使用的代码下面给出:

public class TabBarActivity extends TabActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.main); 

     // Your Tab Titles 
     String tab_title[] = { "Screen 1", "Screen 2"}; 

     // Your Tab Drawables for their states 
     int tab_drawables[] = { R.drawable.tab_home_custom, 
       R.drawable.tab_balance_custom}; 

     // Your Tab Activities 
     Object tab_act[] = { TabB.class,TabC.class}; 

     ///TabHost setup 
     final TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost); 
     tabHost.setup(); 

     TabSpec tab_spec ; 

     for (int i = 0; i < tab_act.length; i++) { 

       tab_spec = tabHost.newTabSpec(tab_title[i]); 
       tab_spec.setIndicator(tab_title[i], 
         getResources().getDrawable(tab_drawables[i])); 
       tab_spec.setContent(new Intent(this, (Class<?>) tab_act[i])); 
       tabHost.addTab(tab_spec); 

     } 
     tabHost.setCurrentTab(0); 
    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (keyCode == KeyEvent.KEYCODE_BACK) { 
      TabBarActivity.this.finish(); 
      return true; 
     } 
     return super.onKeyDown(keyCode, event); 
    } 
} 

Screen Shot

回答

0

只需创建一个新的活动,屏幕0,让它启动你的标签活动,屏幕1和屏幕2,使用意图。然后,您可以更改您的清单文件,以便通过将此代码添加到活动删除中,将您的新活动标记为启动器。

<intent-filter> 
    <action android:name="android.intent.action.MAIN"/> 
    <category android:name="android.intent.category.LAUNCHER"/> 
</intent-filter> 

您还需要从当前选项卡的活动中删除该代码。

0

这不是TabActivity通常会支持的。

您可能能够通过增加一个“虚拟”选项卡,以达到预期的效果:

public class TabBarActivity extends TabActivity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.main); 

    // Your Tab Titles 
    String tab_title[] = { "", "Screen 1", "Screen 2"}; 

    // Your Tab Drawables for their states 
    int tab_drawables[] = { R.drawable.tab_home_custom, 
      R.drawable.tab_balance_custom}; 

    // Your Tab Activities 
    Object tab_act[] = {Activity.class, TabB.class,TabC.class}; 

    ///TabHost setup 
    final TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost); 
    tabHost.setup(); 

    TabSpec tab_spec ; 

    for (int i = 0; i < tab_act.length; i++) { 

      tab_spec = tabHost.newTabSpec(tab_title[i]); 
      if(i>0) tab_spec.setIndicator(tab_title[i], 
        getResources().getDrawable(tab_drawables[i])); 
      tab_spec.setContent(new Intent(this, (Class<?>) tab_act[i])); 
      tabHost.addTab(tab_spec); 

    } 
    tabHost.setCurrentTab(0); 
} 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK) { 
     TabBarActivity.this.finish(); 
     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 

}

我不知道这是否会在标签栏创建一个空白标签按钮虽然

+0

它不是为我工作的。 – MidhunVP 2013-03-06 12:44:40

0

我想你要与应用徽标闪屏,你需要创建另一个活动,并从

public class splash2 extends Activity{ 


    protected boolean _active = true; 
    protected int _splashTime = 2000; 
    Intent intent; 
    Runnable r; 
    Handler handler; 
    /** Called when the activity is first created. */ 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (keyCode == KeyEvent.KEYCODE_MENU) { 
      getWindow().setSoftInputMode(
        WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
      return true; 
     } 
     return super.onKeyDown(keyCode, event); 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.splashlayout2); 

     // startActivity(new Intent(splash2.this,splash1.class)); 
     // thread for displaying the SplashScreen 

     handler = new Handler(); 

     r=new Runnable() { 
      public void run() { 

       intent = new Intent(splash2.this, TabBarActivity .class); 
       //intent.setClass(); 
       startActivity(intent); 

       finish(); 
      } 

     }; 
     handler.postDelayed(r, _splashTime); 

    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     if (event.getAction() == MotionEvent.ACTION_DOWN) { 
      handler.removeCallbacks(r); 
      intent = new Intent(); 
      intent.setClass(splash2.this, Main_menu.class); 
      startActivity(intent); 
      overridePendingTransition(R.anim.slide_in_left2, R.anim.slide_out_left2); 
      finish(); 
     } 
     return true; 
    } 

    @Override 
    public void onBackPressed() { 
     // TODO Auto-generated method stub 

    } 
} 
导航到tabActivity

和下面的标签添加到该活动的活动标签,而不是具有TabBarActivity内这些标签

<intent-filter> 
    <action android:name="android.intent.action.MAIN"/> 
    <category android:name="android.intent.category.LAUNCHER"/> 
</intent-filter> 

它将使这个闪屏默认的活动

+0

我只从另一个登录屏幕到达Tabactivity屏幕。那样的话我该怎么办? – MidhunVP 2013-03-06 12:45:32

+0

你可以发布你想要的截图吗 – 2013-03-06 12:47:57

+0

我通过编辑问题添加了屏幕截图。一旦应用程序到达这个特定的屏幕,它应该显示屏幕0.一旦用户点击第一个标签,它应该显示屏幕1。 – MidhunVP 2013-03-06 13:48:46