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

  • 相关阅读:
    C语言编译多文件
    vs(visual studio 2019)恢复默认设置
    everything 有文件搜不到
    potplayer显示右侧插入列表消息
    ubuntu 关机、重启命令
    post&get请求总结
    C# string格式的日期时间字符串转为DateTime类型
    css position: absolute、relative详解
    在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求
    ASP.NET获取客户端及服务器的信息
  • 原文地址:https://www.cnblogs.com/lzh-Linux/p/4415885.html
Copyright © 2011-2022 走看看