Android学习笔记12---Fragment

来源:互联网 发布:算法是什么意思 编辑:程序博客网 时间:2024/05/22 15:05

先看一个小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

原创粉丝点击