zoukankan      html  css  js  c++  java
  • 用fragment创建一个选项卡

    MainActivity

    public class MainActivity extends FragmentActivity implements OnClickListener {

     private TextView tab1, tab2, tab3, tab4;

     private FragmentManager fragmentManager;

     private FragmentTransaction transaction;

     @Override

     protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);

      tab1 = (TextView) this.findViewById(R.id.tab1);

      tab2 = (TextView) this.findViewById(R.id.tab2);

      tab3 = (TextView) this.findViewById(R.id.tab3);

      tab4 = (TextView) this.findViewById(R.id.tab4);

      tab1.setOnClickListener(this);

      tab2.setOnClickListener(this);

      tab3.setOnClickListener(this);

      tab4.setOnClickListener(this);

      fragmentManager = getSupportFragmentManager();

      transaction = fragmentManager.beginTransaction();

      transaction.replace(R.id.content, new Fragment1());

      transaction.commit();

     }

     @Override

     public void onClick(View arg0) {

      transaction = fragmentManager.beginTransaction();

      switch (arg0.getId()) {

      case R.id.tab1:

       transaction.replace(R.id.content, new Fragment1());

       break;

      case R.id.tab2:

       transaction.replace(R.id.content, new Fragment2());

       break;

      case R.id.tab3:

       transaction.replace(R.id.content, new Fragment3());

       break;

      case R.id.tab4:

       transaction.replace(R.id.content, new Fragment4());

       break;

      }

      transaction.commit();

     }

    }

    四个Fragment

    public class Fragment1 extends Fragment {

     @Override

     public View onCreateView(LayoutInflater inflater, ViewGroup container,

       Bundle savedInstanceState) {

      return inflater.inflate(R.layout.fragment1, null);

     }

    }

    public class Fragment2 extends Fragment {

     @Override

     public View onCreateView(LayoutInflater inflater, ViewGroup container,

       Bundle savedInstanceState) {

      return inflater.inflate(R.layout.fragment2, null);

     }

    }

    public class Fragment3 extends Fragment {

     @Override

     public View onCreateView(LayoutInflater inflater, ViewGroup container,

       Bundle savedInstanceState) {

      return inflater.inflate(R.layout.fragment3, null);

     }

    }

    public class Fragment4 extends Fragment {

     @Override

     public View onCreateView(LayoutInflater inflater, ViewGroup container,

       Bundle savedInstanceState) {

      return inflater.inflate(R.layout.fragment4, null);

     }

    }

    布局文件

    activity_main

    <LinearLayout 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:orientation="vertical"

        tools:context=".MainActivity" >

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:gravity="center"

            android:orientation="horizontal" >

            <TextView

                android:id="@+id/tab1"

                android:layout_width="0dp"

                android:layout_height="wrap_content"

                android:layout_weight="1"

                android:gravity="center"

                android:text="体育新闻" />

            <TextView

                android:id="@+id/tab2"

                android:layout_width="0dp"

                android:layout_height="wrap_content"

                android:layout_weight="1"

                android:gravity="center"

                android:text="社会新闻" />

            <TextView

                android:id="@+id/tab3"

                android:layout_width="0dp"

                android:layout_height="wrap_content"

                android:layout_weight="1"

                android:gravity="center"

                android:text="国内新闻" />

            <TextView

                android:id="@+id/tab4"

                android:layout_width="0dp"

                android:layout_height="wrap_content"

                android:layout_weight="1"

                android:gravity="center"

                android:text="国际新闻" />

        </LinearLayout>

        <LinearLayout

            android:id="@+id/content"

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:gravity="center"

            android:orientation="vertical" >

        </LinearLayout>

    </LinearLayout>

    四个Fragment布局文件

    <?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:gravity="center"

        android:orientation="vertical" >

        <TextView

            android:id="@+id/textView1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="体育新闻" />

    </LinearLayout>

  • 相关阅读:
    Log4Net 全方位跟踪程序运行
    ASP.NET MVC 3和Razor中的@helper 语法
    C# 4.0四大新特性代码示例与解读
    程序员必读
    重学算法(1)--遍历二叉树
    重学算法-目录
    Epplus使用技巧
    JQuery 获取URL中传递的参数
    Epplus 使用案例
    .net调用存储过程详解(转载)
  • 原文地址:https://www.cnblogs.com/freenovo/p/4469787.html
Copyright © 2011-2022 走看看