如何在页面标题栏中放置更多视图?

时间:2012-12-26 17:45:59

标签: android android-layout

我正在尝试使用页面标题条,我需要在我的一个片段中放置三个搜索栏。这是我的代码:

public class MainActivity extends FragmentActivity {

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a
 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
 * will keep every loaded fragment in memory. If this becomes too memory
 * intensive, it may be best to switch to a
 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */
SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;

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

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

}

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

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.
        Fragment fragment = null;
        Bundle args = new Bundle();
        switch (position) {
        case 0:
            fragment = new Page1();
            args.putInt(Page1.ARG_SECTION_NUMBER, position + 1);
            break;
        case 1:
            fragment = new Page2();
            args.putInt(Page2.ARG_SECTION_NUMBER, position + 1);
            break;
        case 2:
            fragment = new Page3();
            args.putInt(Page3.ARG_SECTION_NUMBER, position + 1);
            break;
        }
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
        case 0:
            return getString(R.string.title_section1).toUpperCase();
        case 1:
            return getString(R.string.title_section2).toUpperCase();
        case 2:
            return getString(R.string.title_section3).toUpperCase();
        }
        return null;
    }
}

/**
 * A dummy fragment representing a section of the app, but that simply
 * displays dummy text.
 */
public static class Page1 extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public Page1() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Create a new TextView and set its text to the fragment's section
        // number argument value.
        // TextView textView = new TextView(getActivity());
        // textView.setGravity(Gravity.CENTER);
        // textView.setText(Integer.toString(getArguments().getInt(
        // ARG_SECTION_NUMBER)));
        SeekBar sb1 = new SeekBar(getActivity());
        SeekBar sb2 = new SeekBar(getActivity());
        SeekBar sb3 = new SeekBar(getActivity());
        return sb1;
    }
}

public static class Page2 extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public Page2() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Create a new TextView and set its text to the fragment's section
        // number argument value.
        TextView textView = new TextView(getActivity());
        textView.setGravity(Gravity.LEFT);
        textView.setText("Second tab");
        return textView;
    }
}

public static class Page3 extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public Page3() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Create a new TextView and set its text to the fragment's section
        // number argument value.
        TextView textView = new TextView(getActivity());
        textView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.RIGHT);
        textView.setText("third tab");
        return textView;
    }
}
}

这实际上有效,但我在第一页只有一个搜索栏。何我可以把他们三个???感谢!!!!

1 个答案:

答案 0 :(得分:1)

关于此代码:

    SeekBar sb1 = new SeekBar(getActivity());
    SeekBar sb2 = new SeekBar(getActivity());
    SeekBar sb3 = new SeekBar(getActivity());
    return sb1;

你正在创建3个SeekBars,但只返回第一个。其他两个在返回声明后很快被垃圾收集。 在onCreateView方法内部,您必须创建一个GroupView,并且该组将具有3个SeekBars或您需要的任何布局。最好/最简单的方法是创建XML布局并使用在函数上传递的LayoutInflater。类似于:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    return inflater.inflate(R.layout.frag_page1, null);
}