Android的Fragment的第一種聲明方式

Android的Frangment的第一種聲明方式

實際效果圖如下:

項目結構圖如下:

fragment1:
package com.demo.fragementfirst;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class fragment1 extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


//        通過打氣筒把一個佈局轉換成一個view對象
        View view = inflater.inflate(
                R.layout.fragment_fragment1, null
        );


        return view;


    }
}

MainActivity:
package com.demo.fragementfirst;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
}

MyFragment:
package com.demo.fragementfirst;


import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


//定義fragment
public class MyFragment extends Fragment {


    //    當第一次畫ui的時候調用,通過這個方法可以讓fragment顯示自己的佈局內容
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

//        通過打氣筒把一個佈局轉換成一個view對象
        View view = inflater.inflate(
                R.layout.myfragment, null
        );


        return view;

    }
}

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <!--viewgroup可以有自己的孩子
    通過 oncreateview 這個方法 fragment可以加載自己的佈局


    -->
    <fragment
        android:name="com.demo.fragementfirst.MyFragment"
        android:id="@+id/list"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        >
    </fragment>

    <fragment
        android:name="com.demo.fragementfirst.fragment1"
        android:id="@+id/viewer"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        ></fragment>



</LinearLayout>
fragment_fragment1.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".fragment1">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="這是第二個fragemnt" />

</FrameLayout>
myfragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".fragment1">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#D64B28"
        android:textColor="#FFFFFF"
        android:text="這個是第一個fragment" />

</FrameLayout>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章