zoukankan      html  css  js  c++  java
  • android 之fragment创建

    1.使用xml标签

    1.1定义两个重要属性

       <fragment
            android:id="@+id/fregment_top"
            android:layout_width="match_parent"
            android:layout_height="220dp"
            android:layout_marginTop="80dp"
            android:name="com.lzh.fragment_study.Framgment_first"
            />

    1.2实现一个继承自Fragment的新类

    public class Framgment_first extends Fragment {

            public Framgment_first() {
            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_main, container,
                        false);
                return rootView;
            }
        }

    1.3创建一个Fragment的layout文件 fragment_main

    <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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.lzh.fragment_study.MainActivity$PlaceholderFragment" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world"
            />

    </RelativeLayout>

    2.通过JAVA代码实现

    2.1在程序中加入Fragment
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            ExampleFragment fragment = new ExampleFragment();
            fragmentTransaction.add(R.id.container, fragment);
            fragmentTransaction.commit();

    getSupportFragmentManager().beginTransaction()
                    .add(R.id.container,new Framgment_first()).commit();

    2.2动态移除或添加fragment

        getSupportFragmentManager()
                        .beginTransaction()
                        .remove(frag).commit();


     getSupportFragmentManager().beginTransaction()
                        .add(R.id.container,frag).commit();

  • 相关阅读:
    jmeter接口测试二
    jmeter 插件入口
    Python正则匹配中的最小匹配和贪婪匹配
    python中的url编码和解码(encode与decode)乱码
    python2.7+pyqt+eric基本控件操作(制作界面化程序)
    python2.7+PyQt4+eric6 界面开发环境配置
    centos配置静态ip地址
    分片,步长,索引
    我看过的几本书籍
    软件测试工程师的成长之路(个人看法)
  • 原文地址:https://www.cnblogs.com/lzh-Linux/p/4415885.html
Copyright © 2011-2022 走看看