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);
    }
} 

截屏

4

3 回答 3

0

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

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

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

于 2013-03-06T11:22:23.657 回答
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);
}

}

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

于 2013-03-06T11:23:27.473 回答
0

我想您想要一个带有应用程序徽标的启动画面,您需要创建另一个活动并从中导航到 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>

它将使此启动屏幕成为默认活动

于 2013-03-06T11:30:53.030 回答