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>
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); }}
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
- Android学习笔记12---Fragment
- Android Fragment学习笔记
- android fragment学习笔记
- 学习笔记 android fragment
- android Fragment 学习笔记
- Android学习笔记 - fragment
- Android学习笔记 Fragment
- android学习笔记之Fragment
- Android 学习笔记13:Fragment
- Fragment生命周期---Android学习笔记
- Android学习笔记之fragment
- Android Fragment的学习笔记
- Android中Fragment学习笔记
- Android学习笔记--Fragment基础
- Android学习笔记之Fragment
- Android Fragment学习笔记(二)----Fragment界面添加和管理
- Android Fragment学习笔记(二)----Fragment界面添加和管理
- Android Fragment学习笔记(二)----Fragment界面添加和管理
- 用phpstrom编辑器编辑linux服务器上的远程项目
- Quartus引脚分配【转载】
- ajax实现上传文件
- 正则表达式的常用表示方法
- PHP的冒泡排序
- Android学习笔记12---Fragment
- Java字符串String的解析
- 1-wire单总线浅谈之(一)--协议时序
- QT 下调用lua自定义模块
- error C3872: “0x3000”: 此字符不允许在标识符中使用错误 的解决办法
- 递归-OpenJudge-1750:全排列
- Ajax提交与传统表单提交的区别说明
- 深入了解 WPF Dispatcher 的工作原理(PushFrame 部分)
- CodeForces 864F Cities Excursions