Android學習筆記12---Fragment

先看一個小demo,再來解釋Fragment

fragment_title.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:background="@drawable/title_bar" >

    <!-- 控件用dp -->
    <ImageButton
        android:id="@+id/id_title_btn"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerVertical="true"
        android:background="@drawable/title_bar" />

</RelativeLayout>

fragment_content.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"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:text="I am Content Fragment"
        android:textSize="20sp"
        android:textStyle="bold" />

</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.amy.fragmenttest.MainActivity">

    <!--why?
            在fragment裏面要對TitleFragment和ContentFragment類進行註冊
        並設置相應的id?
        answer:
        <fragment>中的android:name指明瞭具體的Fragment類。
            當系統創建Activity的佈局時,它會檢查每個<fragment>,並調用指明的Fragment類的onCreateView方法。
        當onCreateView返回一個View對象後,系統會用該View替換<fragment ...>標籤指代的內容。

        Note:每個Fragment都需要一個唯一標識符ID,用來在Activity restart 的時候恢復Fragment。有三種方法可以提供ID:
        (1)android:id                     a unique ID
        (2)android:tag                  a unique string
        (3)container view的ID      當(1)(2)都沒有設置的話
        -->
    <fragment
        android:id="@+id/id_fragment_title"
        android:name="com.amy.fragmenttest.TitleFragment"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        />
    <fragment
        android:id="@+id/id_fragment_content"
        android:name="com.amy.fragmenttest.ContentFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</android.support.constraint.ConstraintLayout>


TitleFragment.java

package com.amy.fragmenttest;

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

public class TitleFragment extends Fragment {
    private ImageButton imageButton;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_title,container,false);
        imageButton = (ImageButton) view.findViewById(R.id.id_title_btn);
        //爲標題圖片按鈕設置一個監聽事件
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),"I'm TitleFragment!",Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }
}
ContentFragment.java

package com.amy.fragmenttest;

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

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


MainActivity.java

package com.amy.fragmenttest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
    }
}

效果圖,走你:


來解釋一下:

http://blog.csdn.net/lmj623565791/article/details/37970961

Fragment跟Activity很像,以上面demo爲例:

Activity被分割成兩個Fragment碎片,一個用於表示標題,一個用於表示內容。

需要注意是:

1.TitleFragment與ContentFragment都必須繼承Fragment類,並重寫相應的onCreate方法

2.需要在activity_main.xml文件裏配置Fragment類

3.每個Fragment都需要一個唯一的標識符ID

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