标题不会显示在SlidingTabLayout中

时间:2014-11-30 01:55:45

标签: java android

我曾尝试将SlidingTabLayout实现到我的应用中。可悲的是,它没有在滑动标签中显示标题。我需要在选项卡中显示我的活动的标题(后面的图标)。我可以知道吗?我对android编程很新。这是我的代码:

MainMenuActivity.java

import com.example.android.common.view.SlidingTabLayout;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainMenuActivity extends ActionBarActivity {

    private PagerAdapter mPagerAdapter;
    private SlidingTabLayout mSlidingTabLayout;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
        }

        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this,EducationActivity.class.getName()));
        fragments.add(Fragment.instantiate(this,EducationActivity.class.getName()));

        mPagerAdapter = new PagerAdapter(this.getSupportFragmentManager(), fragments);
        ViewPager pager = (ViewPager) findViewById(R.id.viewpager);     
        pager.setAdapter(mPagerAdapter);
        mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
        mSlidingTabLayout.setViewPager(pager);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        else if (id == R.id.action_disclaimer) {
            Intent intent = new Intent(this, DisclaimerActivity.class);
            startActivity(intent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

PagerAdapter.java

import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class PagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments;

    public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        // TODO Auto-generated constructor stub
        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int arg0) {
        // TODO Auto-generated method stub

        return this.fragments.get(arg0);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return this.fragments.size();
    }

}

EducationActivity.java

public class EducationActivity extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View v = inflater.inflate(R.layout.activity_education,
                container,false);

        Button btn_warfarin = (Button)v.findViewById(R.id.btn_warfarin);
        btn_warfarin.setOnClickListener(new OnClickListener()
        {
             public void onClick(View v)
             {
                Intent intent = new Intent(getActivity(), WarfarinInfoActivity.class);
                startActivity(intent);

             } 

        });     
}

我从http://developer.android.com/读过SlidingTabsBasicFragment.java,但我不太了解它。希望有人能指导我。感谢。

1 个答案:

答案 0 :(得分:2)

你错过了

@Override
public CharSequence getPageTitle(int position) {

    return myStringArrayOfTabs[position];
}
从您的适配器

,根据需要填写标题。