1

我有以下课程:

 public class MyFragment extends Fragment{

    int mCurrentPage;

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

        /** Getting the arguments to the Bundle object */
        Bundle data = getArguments();

        /** Getting integer data of the key current_page from the bundle */
        mCurrentPage = data.getInt("current_page", 0);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {     

        View v = inflater.inflate(R.layout.myfragment_layout, container,false);             
        TextView tv = (TextView ) v.findViewById(R.id.tv);
        tv.setText("Você está no item " + mCurrentPage +" do menu"+ "\n\n" + "deslize para os lados para poder escolher a opção");  
        return v;

oncreateview 方法只调用一个布局,我如何在每个页面或滑动视图选项卡中将其称为布局(片段),主要用作菜单。

梨作为 Bandle 方法 paraâmentro 以下类:

public class MyFragmentPagerAdapter extends FragmentPagerAdapter{

    final int PAGE_COUNT = 5;

    /** Constructor of the class */
    public MyFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    /** This method will be invoked when a page is requested to create */
    @Override
    public Fragment getItem(int arg0) {

        MyFragment myFragment = new MyFragment();
        Bundle data = new Bundle();
        data.putInt("current_page", arg0+1);
        myFragment.setArguments(data);
        return myFragment;
    }

    /** Returns the number of pages */
    @Override
    public int getCount() {     
        return PAGE_COUNT;
    }

    @Override
    public CharSequence getPageTitle(int position) {        
        return "Pag" + ( position + 1 );
    }

主要看起来像这样:

public class MainActivity extends FragmentActivity {

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

        /** Obtendo uma referência ao ViewPager definido o arquivo de layout */        
        ViewPager pager = (ViewPager) findViewById(R.id.pager);

        /** Obtendo gerente fragmento */
        FragmentManager fm = getSupportFragmentManager();

        /** instanciando FragmentPagerAdapter */
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);

        /** definindo o pagerAdapter ao objeto pager */
        pager.setAdapter(pagerAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

和或 xml main_activity:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <android.support.v4.view.PagerTabStrip
            android:id="@+id/pager_tab_strip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:background="#33b5e5"
            android:paddingBottom="4dp"
            android:paddingTop="4dp"
            android:textColor="#fff" />
    </android.support.v4.view.ViewPager>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="46dp"
        android:text="Menu Principal"
        android:textColor="#1E90FF"
        android:textStyle="bold"
        android:typeface="sans"
        android:textSize="25sp" />

</RelativeLayout>

和 myfragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tv"    
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:gravity="center"    
    >    
</TextView>   

请给我发送代码,这些代码可以实现我绘制多个布局的目标!

4

0 回答 0