zoukankan      html  css  js  c++  java
  • Fragment的2中载入方式!

    1、静态有动态

    代码如下:

    public class MainActivity extends AppCompatActivity {
        private ContentFragment cf;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_main);
            setFragment();
    
        }
        //动态载入
        public void setFragment(){
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            cf =  new ContentFragment();
            ft.replace(R.id.fragment_content,cf);
            ft.commit();
        }
    }

    静态载入fragment布局
    title_layout.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="40dp"
                android:text="@string/title"/>
    
    </LinearLayout>

    动态载入的布局:
    context_layout.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">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="40dp"
            android:text="@string/context"/>
    </LinearLayout>

    activity_main.xml布局

    <?xml version="1.0" encoding="utf-8"?>
    <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.lesson.hs.fragmentExp.MainActivity">
        <!-- 自己定义的fragment-->
        <fragment
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/fragment_title"
            android:name="com.lesson.hs.fragmentExp.TitleFragment"
            />
        <!-- 动态载入的fragment容器-->
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/fragment_title"
            android:id="@+id/fragment_content"
            />
    </RelativeLayout>

    动态载入的fragment类:

    public class ContentFragment extends Fragment {
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.context_layout,container,false);
            return view;
        }
    }

    静态载入的fragment类:

    public class TitleFragment extends Fragment {
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.title_layout,container,false);
            return view;
        }
    }
    这个只不过是自己的流水账,偶尔有一些心得,错误的地方概不负责
  • 相关阅读:
    传Windows 7 RC泄露版中含有木马 狼人:
    金山:3G时代 上网安全面临更大挑战 狼人:
    McAfee:僵尸网新威胁远甚Conficker 狼人:
    安全专家:70GB财务数据被僵尸网络盗窃 狼人:
    卡巴斯基中国地区4月恶意软件排行榜 狼人:
    MPAA组织遭遇尴尬 网页存在XSS攻击漏洞 狼人:
    股市回暖 网上炒股安全风险骤增 狼人:
    微软首次针对Windows 7推杀毒软件 年内将推出 狼人:
    黑客数度入侵美国联邦航空总署飞航控制系统 狼人:
    瑞星对Windows7捆绑杀毒软件等消息的回应 狼人:
  • 原文地址:https://www.cnblogs.com/ashitaka/p/5921085.html
Copyright © 2011-2022 走看看