• Android入门——UI(7)——Fragment


    先上fragment静态加载的代码

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:text="这是一个Fragment"
            android:background="#0f0"/>
    </LinearLayout>
    fragment_index.xml
    package com.ouc.wkp.ui1;
    
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    /**
     * Created by wkp on 2016/8/25.
     */
    public class FragmentIndex extends Fragment {//app包下3.0以后才可以使用
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.fragment_index,container);
            return view;
        }
    }
    FragmentIndex.java

    注意继承的Fragment最好是

    import android.support.v4.app.Fragment;  这样可以兼容早期的安卓api

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <fragment
            android:id="@+id/fragment1"
            android:name="com.ouc.wkp.ui1.FragmentIndex"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    fragment_demo.xml

    注意fragment元素需要指定id

    package com.ouc.wkp.ui1;
    
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.FragmentActivity;
    
    /**
     * Created by wkp on 2016/8/25.
     */
    public class FragmentDemo extends FragmentActivity{
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.fragment_demo);
        }
    }
    FragmentDemo.java

    然后是动态加载

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent" android:background="#f00">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="这是一个Fragment,通过动态方式加载"
            />
    </LinearLayout>
    fragment_index.xml
    package com.ouc.wkp.ui1;
    
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    /**
     * Created by wkp on 2016/8/25.
     */
    public class FragmentIndex extends Fragment {//app包下3.0以后才可以使用
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            //别遗忘第三个false参数,否则会出现 java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
            View view=inflater.inflate(R.layout.fragment_index,container,false);
            return view;
        }
    }
    FragmentIndex.java

    注意代码中的那个注释

    <?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:orientation="vertical">
    
        <Button
            android:id="@+id/btn_addd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="增加" />
    
        <Button
            android:id="@+id/btn_deletee"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="减少" />
    
        <FrameLayout
            android:id="@+id/frame_layout"
            android:layout_width="match_parent"
            android:layout_height="200dp"></FrameLayout>
    </LinearLayout>
    fragment_demo.xml
    package com.ouc.wkp.ui1;
    
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import android.view.View;
    
    /**
     * Created by wkp on 2016/8/25.
     */
    public class FragmentDemo extends FragmentActivity{
    
        FragmentIndex fragmentIndex;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.fragment_demo);
    
            fragmentIndex=new FragmentIndex();
    
            findViewById(R.id.btn_addd).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    FragmentManager fm=getSupportFragmentManager();
                    FragmentTransaction ft=fm.beginTransaction();
    
                    ft.replace(R.id.frame_layout,fragmentIndex);
                    ft.commit();
                }
            });
    
            findViewById(R.id.btn_deletee).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    FragmentManager fm=getSupportFragmentManager();
                    FragmentTransaction ft=fm.beginTransaction();
    
                    ft.remove(fragmentIndex);
                    ft.commit();
                }
            });
        }
    }
    FragmentDemo.java

    效果图 点击增加出现红块 点击减少消失

  • 相关阅读:
    格式化日期---获取年月日升级版
    时间格式转换
    Python求两个有序数组的中位数的几种方法
    pyinstaller打包时包含资源文件
    PyQt5自定义组件之飞机水平仪
    Python获取磁盘剩余空间
    PyQt5自定义组件之信号强度
    Python字典的实现原理
    获取元素相对浏览器窗口的偏移坐标
    HTML/JavaScript实现地图以鼠标为圆心缩放和移动
  • 原文地址:https://www.cnblogs.com/wangkaipeng/p/5805239.html
Copyright © 2020-2023  润新知