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

    本文结合之前的动态创建fragment来进行一个实践,来实现用Fragment创建一个选项卡

    本文地址:http://www.cnblogs.com/wuyudong/p/5898075.html ,转载请注明源地址。

    项目布局

    <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="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <TextView
                android:id="@+id/tab1"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="社会新闻" />
    
            <TextView
                android:id="@+id/tab2"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="生活新闻" />
    
            <TextView
                android:id="@+id/tab3"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="军事新闻" />
    
            <TextView
                android:id="@+id/tab4"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="娱乐新闻" />
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/content"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </LinearLayout>
    
    </LinearLayout>

    新建Fragment1.java~Fragment4.java,其中Fragment1.java中的代码如下:

    public class Fragment1 extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment1, null);
        }
    
    }

    其他几个文件的代码类似

    新建fragment1.xml~fragment4.xml,其中fragment1.xml中的代码如下:

    <?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="社会新闻" 
            android:textAppearance="?android:attr/textAppearanceLarge"/>
    </LinearLayout>

    其他几个文件的代码类似

    MainActivity.java中的代码如下:

    public class MainActivity extends Activity implements OnClickListener {
    
        private LinearLayout content;
        private TextView tv1, tv2, tv3, tv4;
        private FragmentManager fm;
        private FragmentTransaction ft;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            content = (LinearLayout) findViewById(R.id.content);
    
            tv1 = (TextView) findViewById(R.id.tab1);
            tv2 = (TextView) findViewById(R.id.tab2);
            tv3 = (TextView) findViewById(R.id.tab3);
            tv4 = (TextView) findViewById(R.id.tab4);
    
            tv1.setOnClickListener(this);
            tv2.setOnClickListener(this);
            tv3.setOnClickListener(this);
            tv4.setOnClickListener(this);
    
            fm = getFragmentManager();
            ft = fm.beginTransaction();
            ft.replace(R.id.content, new Fragment1()); // 默认情况下Fragment1
    
        }
    
        @Override
        public void onClick(View v) {
            ft = fm.beginTransaction();
            switch (v.getId()) {
            case R.id.tab1:
                ft.replace(R.id.content, new Fragment1());
                break;
            case R.id.tab2:
                ft.replace(R.id.content, new Fragment2());
                break;
            case R.id.tab3:
                ft.replace(R.id.content, new Fragment3());
                break;
            case R.id.tab4:
                ft.replace(R.id.content, new Fragment4());
                break;
    
            default:
                break;
            }
            ft.commit();
    
        }
    
    }

    运行项目后如下效果:

  • 相关阅读:
    首次远程安装 GlassFish 后以远程 Web 方式访问其后台管理系统出现错误的解决方法(修订)
    在 Mac OS X 环境中从源代码编译安装 FFmpeg
    编译 Android 版本的 Opus 音频编解码库的方法
    在 NetBeans 中开发一般 Java 应用程序时配置 Allatori 进行代码混淆
    使用 IntelliJ IDEA 开发一般 Java 应用程序时配置 Allatori 进行代码混淆
    使用 IntelliJ IDEA 开发 Android 应用程序时配置 Allatori 进行代码混淆
    基于现有图像数据创建自定义像素格式的 BufferedImage
    AppCode 中开发 Mac OS X 应用程序或共享库的经验小结
    MinGW 创建的程序或 DLL 脱离 libgcc-xx-xx.dll 和 libstdc++-x.dll 运行库的方法
    MinGW 使用和创建 DLL 应注意的问题
  • 原文地址:https://www.cnblogs.com/wuyudong/p/5898075.html
Copyright © 2011-2022 走看看