将布局附加到标签 - Android

时间:2013-12-10 09:39:46

标签: android tabs

我在我的Android应用中使用PagerTabStrip,就像在新的Google Play商店应用中使用的那样。我经历了一些教程,并成功创建了三个选项卡。它们信息电子 配置事实

信息

电子配置

事实

这是 xml布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<android.support.v4.view.PagerTabStrip
    android:id="@+id/pager_title_strip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:textSize="30dp"
    android:background="#000000"
    android:paddingBottom="4dp"
    android:paddingTop="4dp"
    android:textColor="#ffffff" />

</android.support.v4.view.ViewPager>

这是 Java文件

public class Tabs extends FragmentActivity 
{
    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act2aluminium);
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        }

    public class SectionsPagerAdapter extends FragmentPagerAdapter 
    {
        public SectionsPagerAdapter(FragmentManager fm) 
        {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) 
        {
            Fragment fragment = new DummySectionFragment();
            Bundle args = new Bundle();
            args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public int getCount() 
        {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
        switch (position) 
        {
            case 0:
                return "Information";
            case 1:
                return "Electronic Configuration";
            case 2:
                return "Facts";
            }
        return null;
        }
    }

    public static class DummySectionFragment extends Fragment {
    public static final String ARG_SECTION_NUMBER = "section_number";
    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        TextView textView = new TextView(getActivity());
        textView.setGravity(Gravity.CENTER);
        textView.setText(Integer.toString(getArguments().getInt(
                ARG_SECTION_NUMBER)));
        return textView;
        }
    }
}

现在,我的问题是如何将布局或页面附加到标签而不是微小的1,2,3文本视图?

我搜索了很多但是没有找到一个很好的解释它是如何完成的。请帮我解决问题。提前谢谢!

1 个答案:

答案 0 :(得分:2)

<强> activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<android.support.v4.view.PagerTabStrip
    android:id="@+id/pager_title_strip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:textSize="30dp"
    android:background="#000000"
    android:paddingBottom="4dp"
    android:paddingTop="4dp"
    android:textColor="#ffffff" />

</android.support.v4.view.ViewPager>

<强> page1.xml

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Click here" />

<TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_below="@id/button1"
    android:text="I am Page one" />

<强> page2.xml

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:layout_centerInParent="true"
    android:text="Click here" />

<TextView
    android:id="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_below="@id/button2"
    android:text="I am Page two" />

<强> page3.xml

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:layout_centerInParent="true"
    android:text="Click here" />

<TextView
    android:id="@+id/text3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_below="@id/button3"
    android:text="I am Page three" />

<强> MainActivity.java

public class MainActivity extends FragmentActivity 
{
    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        }

    public class SectionsPagerAdapter extends FragmentPagerAdapter 
    {
        public SectionsPagerAdapter(FragmentManager fm) 
        {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) 
        {

             switch (position) {
             case 0:
                 // Top Rated fragment activity
                 return new Information();
             case 1:
                 // Games fragment activity
                 return new ElectonicConfiguration();
             case 2:
                 // Movies fragment activity
                 return new Fact();
             }

             return null;
        }

        @Override
        public int getCount() 
        {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
        switch (position) 
        {
            case 0:
                return "Information";
            case 1:
                return "Electronic Configuration";
            case 2:
                return "Facts";
            }
        return null;
        }
    }

    //Page 1 Fragment
    public static class Information extends Fragment {


    public Information()
        {
        }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
         View view = inflater.inflate(R.layout.page1, null);
         TextView textView = (TextView)view.findViewById(R.id.text1);
         Button button=(Button)view.findViewById(R.id.button1);

       return view ;
        }
    }

    //Page 2 Fragment
    public static class ElectonicConfiguration extends Fragment {


        public ElectonicConfiguration()
            {
            }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
             View view = inflater.inflate(R.layout.page2, null);
             TextView textView = (TextView)view.findViewById(R.id.text2);
             Button button=(Button)view.findViewById(R.id.button2);

           return view ;
            }
        }

    //Page 3 Fragment
    public static class Fact extends Fragment {

        public Fact()
            {
            }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
             View view = inflater.inflate(R.layout.page3, null);
             TextView textView = (TextView)view.findViewById(R.id.text3);
             Button button=(Button)view.findViewById(R.id.button3);

           return view ;
            }
        }
}

page1.xmlpage2.xmlpage3.xml分别是第一页,第二页和第三页的布局文件。在MainActivity.java中声明了3个不同的片段3个不同的页面。 getItem()类的SectionsPagerAdapter管理所有3个片段页面。根据您的意愿在xml文件中进行更改。我认为代码非常自我解释。如果您有任何疑问,请不要犹豫。

希望它有所帮助。干杯!