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();
    
        }
    
    }

    运行项目后如下效果:

  • 相关阅读:
    MS17-010(永恒之蓝)远程溢出漏洞
    SQL注入实战之盲注篇(布尔、时间盲注)
    kali下vulhub的使用
    SQL注入实战之报错注入篇(updatexml extractvalue floor)
    SQL注入实战之联合查询篇(含漏洞网站搭建)
    apache/nginx/IIS日记记录分析
    nvcatmysql的安装
    乌云(wooyun)虚拟机的搭建复活
    Web安全基础笔记
    公告、留言
  • 原文地址:https://www.cnblogs.com/wuyudong/p/5898075.html
Copyright © 2011-2022 走看看