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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值