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>

  • 相关阅读:
    【记录】百度统计监控博客园
    【织梦】网站地图创建和美化
    【IDE】JRebel热部署实现
    【字体图标】 Font Awesome字体图标如何使用?
    【Eureka】springCloud项目搭建
    java 服务定期卡顿、卡死,服务在运行没挂,日志疯狂打印,接口不能用
    idea springboot 无法启动 Unable to start EmbeddedWebApplicationContext
    POI导出xlsx
    mysql decimal设置默认值0 无效,设置后自动变为null(通过Navicat可视化工具操作)
    Log file ./ib_logfile2 is of different size 268435456 bytes than other log files 50331648 bytes!
  • 原文地址:https://www.cnblogs.com/freenovo/p/4469787.html
Copyright © 2011-2022 走看看