调试:如何使用自定义选项卡启动活动

时间:2012-10-03 05:54:41

标签: android android-activity tabs android-tabhost

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        icon = (ImageView) findViewById(R.id.header);

        Resources res = getResources();

        // construct the tabhost
        setContentView(R.layout.tab_layout);

        setupTabHost();
        // mTabHost.getTabWidget().setDividerDrawable(R.drawable.hotels);

        setupTab(new TextView(this), "Hotels");
        res.getDrawable(R.drawable.hote);

        setupTab(new TextView(this), "MyAccount");
        res.getDrawable(R.drawable.ma);

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                R.layout.tab_layout);
        View title = getWindow().findViewById(R.id.tabsLayout);

    }

    @TargetApi(4)
    private void setupTab(final View view, final String tag) {
        View tabview = createTabView(mTabHost.getContext(), tag);

        TabSpec set_Content = mTabHost.newTabSpec(tag).setIndicator(tabview)
                .setContent(new TabContentFactory() {
                    public View createTabContent(String tag) {
                        return view;
                    }
                });

        switch (tag) {
        case ("Hotels"): {

            Intent setClass = new Intent(this, ListSample.class);
            set_Content.setContent(setClass);
            break;
        }
        case ("MyAccount"): {

            /*
             * Intent setClass = new Intent(this, AccountActivity.class);
             * set_Content.setContent(setClass);
             */break;
        }
        }

        mTabHost.addTab(set_Content);

    }

    private static View createTabView(final Context context, final String text) {
        View view = LayoutInflater.from(context)
                .inflate(R.layout.tabs_bg, null);
        TextView tv = (TextView) view.findViewById(R.id.tabsText);
        tv.setText(text);
        return view;
    }
}

我正在自定义选项卡并尝试为每个选项卡启动活动。我无法找到错误。

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

 final TabHost tabHost = getTabHost();
    TabHost.TabSpec ts = tabHost.newTabSpec("ID_1");
    ts.setIndicator("1"); 
    ts.setContent(Intent1);// Intent to start activity
    tabHost.addTab(ts);

    ts = tabHost.newTabSpec("ID_2");
    ts.setIndicator("2"); 
    ts.setContent(Intent2);
    tabHost.addTab(ts);

    ts = tabHost.newTabSpec("ID_3");
    ts.setIndicator("3"); 
    ts.setContent(Intent3);

    tabHost.addTab(ts);