Tab Activity第一个屏幕独立于选项卡

时间:2013-03-06 11:13:12

标签: android android-layout android-widget

我的屏幕底部有一个带有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

3 个答案:

答案 0 :(得分:0)

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

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

您还需要从当前标签活动中删除该代码。

答案 1 :(得分: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);
}

}

我不确定这是否会在标签栏中创建一个空白的标签按钮

答案 2 :(得分:0)

我想你想要一个带有app徽标的闪屏,你需要创建另一个活动并导航到tabActivity

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

    }
}

并将以下标记添加到此活动的活动代码中,而不是在TabBarActivity中包含这些标记

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

它会使此启动画面成为默认活动