我需要在tabhost中更改选项卡名称

时间:2016-03-13 07:27:02

标签: android

我是初学者,我需要知道如何更改我的应用程序中的Tab名称和片段名称请帮助我,当我使用字符串并提供选项卡的名称并将逻辑更改为选项卡Names.length然后我的应用程序崩溃说“不幸的应用程序已停止”。请帮我解决这个问题。

这是主要的activity.java代码

package com.tech2dsk.musicdsk;

import java.util.List;
import java.util.Vector;

import com.tech2dsk.adapters.MyFragmentPagerAdapter;
import com.tech2dsk.fragments.Fragment1;
import com.tech2dsk.fragments.Fragment2;
import com.tech2dsk.fragments.Fragment3;
import com.tech2dsk.fragments.Fragment4;
import com.tech2dsk.musicdsk.R;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabContentFactory;

public class MainActivity extends ActionBarActivity implements
        OnTabChangeListener, OnPageChangeListener {

    private TabHost tabHost;
    private ViewPager viewPager;
    private MyFragmentPagerAdapter myViewPagerAdapter;
    int i = 0;

    // fake content for tabhost
    class FakeContent implements TabContentFactory {
        private final Context mContext;

        public FakeContent(Context context) {
            mContext = context;
        }

        @Override
        public View createTabContent(String tag) {
            View v = new View(mContext);
            v.setMinimumHeight(0);
            v.setMinimumWidth(0);
            return v;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        i++;

        // init tabhost
        this.initializeTabHost(savedInstanceState);

        // init ViewPager
        this.initializeViewPager();

    }

    private void initializeViewPager() {
        List<Fragment> fragments = new Vector<Fragment>();

        fragments.add(new Fragment1());
        fragments.add(new Fragment2());
        fragments.add(new Fragment3());
        fragments.add(new Fragment4());


        this.myViewPagerAdapter = new MyFragmentPagerAdapter(
                getSupportFragmentManager(), fragments);
        this.viewPager = (ViewPager) super.findViewById(R.id.viewPager);
        this.viewPager.setAdapter(this.myViewPagerAdapter);
        this.viewPager.addOnPageChangeListener(this);

        onRestart();

    }

    private void initializeTabHost(Bundle args) {

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

        for (int i = 1; i <= 4; i++) {

            TabHost.TabSpec tabSpec;
            tabSpec = tabHost.newTabSpec("Tab " + i);
            tabSpec.setIndicator("Tab " + i);
            tabSpec.setContent(new FakeContent(this));
            tabHost.addTab(tabSpec);
        }
        tabHost.setOnTabChangedListener(this);
    }

    @Override
    public void onTabChanged(String tabId) {
        int pos = this.tabHost.getCurrentTab();
        this.viewPager.setCurrentItem(pos);

        HorizontalScrollView hScrollView = (HorizontalScrollView) findViewById(R.id.hScrollView);
        View tabView = tabHost.getCurrentTabView();
        int scrollPos = tabView.getLeft()
                - (hScrollView.getWidth() - tabView.getWidth()) / 2;
        hScrollView.smoothScrollTo(scrollPos, 0);

    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    @Override
    public void onPageSelected(int position) {
        this.tabHost.setCurrentTab(position);
    }

}

2 个答案:

答案 0 :(得分:0)

在xml里面添加以下代码&lt; TabWidget&gt;

             <TextView
                android:tag="tab0"
                android:text="Tab 1"
                android:background="@android:drawable/btn_star_big_on"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                />
            <TextView
                android:tag="tab1"
                android:text="Tab 2"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                />
            <TextView
                android:tag="tab2"
                android:text="Tab 3"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                />

答案 1 :(得分:0)

如果您想以编程方式执行此操作,请将此方法添加到FragmentPagerAdapter:

@Override
public CharSequence getPageTitle(int position) {
    return translateIntoName(position); //replace this with your logic
}

也可以使用TabLayout

代替tabhost